Javix
2013-Mar-04 21:21 UTC
accepts_nested_attributes: undefined method ''association''_attributes
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 you’re 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/r7cC2l3lvsgJ. For more options, visit https://groups.google.com/groups/opt_out.
Lorenz Blackbird
2013-Mar-05 12:07 UTC
Re: accepts_nested_attributes: undefined method ''association''_attributes
I''m not sure, but I think that you must use "build" method to create a TimeEntry associated to TimeSheet. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Javix
2013-Mar-05 12:25 UTC
Re: accepts_nested_attributes: undefined method ''association''_attributes
On Tuesday, March 5, 2013 1:07:13 PM UTC+1, Ruby-Forum.com User wrote:> > I''m not sure, but I think that you must use "build" method to create a > TimeEntry associated to TimeSheet. > > Sorry , but the question was not about how to create time_entries > association but how to receive the association attributes. I believed that > calling timesheet.time_entries_attributes will do that. >Regards> Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/iD6vjBKIMt4J. For more options, visit https://groups.google.com/groups/opt_out.
Apparently Analagous Threads
- How to use group in nested associations
- validates presence of foreign key fails in nested form
- fields_for with accepts_nested_attributes: how to pass a value to a label
- nested forms: use validate :some_method works in update mode only
- search engine, select the selected option