Search code examples
ruby-on-railsdevisebelongs-to

Issue with belongs_to and user (devise)


I'm new to RoR, and I would like to develop an app, but I have an issue with the belongs_to association. I am using devise for the authentication of my users, and I have an object called timesheet, I followed several tutorials and read a lot of forums but unfortunately user_id remains null in my db, so I do not know where does the problem come from.

If you can tell how to fix it, any links that can helps me, that would be great.

Schema.rb:

ActiveRecord::Schema.define(version: 20150128160116) do

  create_table "timesheets", force: true do |t|
    t.date     "date"
    t.time     "working_start_time"
    t.time     "working_end_time"
    t.integer  "breaks"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "timesheets", ["user_id"], name: "index_timesheets_on_user_id"

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.integer  "current_sign_in_ip"
    t.integer  "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

timesheets_controller.rb

class TimesheetsController < ApplicationController
 layout 'application'
 
def show
  @timesheet=Timesheet.find(params[:id])
end
 
def index
  @timesheet = Timesheet.all
end
  

def new
    @timesheet = Timesheet.new
end

def create
       @timesheet = Timesheet.create(timesheet_params)
       redirect_to new_timesheet_path
end

  def edit
    @timesheet=Timesheet.find(params[:id])
  end
  
    def update
    @timesheet = Timesheet.find(params[:id])
    @timesheet.update_attributes(timesheet_params)
  redirect_to student_table_path
end

user.rb model

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
has_many :timesheets
end

Timesheet.rb model

class Timesheet < ActiveRecord::Base
belongs_to :user
validates :user_id, :presence => true
end

Thanks in advance.


Solution

  • It will stay null because you are not using it in the timesheetsController, your create action should be like this :

    def create
      @timesheet = current_user.timesheets.build(timesheet_params)
      redirect_to new_timesheet_path
    end
    

    You have to use that build method to reference the current_user, so the timesheet will have the current_user in the user_id field.