I have 2 models Timesheet and TimeEntry:
class Timesheet < ActiveRecord::Base
attr_accessible :status, :user_id, :time_entries_attributes
has_many :time_entries, dependent: :destroy
accepts_nested_attributes_for :time_entries, :reject_if => proc {
|attributes| attributes[‘worktime’].blank? }
end
class TimeEntry < ActiveRecord::Base
attr_accessible :task_id, :timesheet_id, :workdate, :worktime
belongs_to :timesheet
end
As explained in Rails API:
Using with attr_accessible
The use of attr_accessible can interfere with nested attributes if youre
not careful. For example, if the Member model above was using
attr_accessible like this:
attr_accessible :name
You would need to modify it to look like this:
attr_accessible :name, :posts_attributes
If I try to do the same for my case with time_entries association:
irb(main):016:0> t = Timesheet.new
=> #<Timesheet id: nil, user_id: nil, status: nil, created_at: nil,
updated_at: nil>
irb(main):017:0> t.time_entries_attributes
NoMethodError: undefined method `time_entries_attributes' for
#<Timesheet:0x007fa8aee56e98>
It does not work. Any idea?
Thanks and regards