I''m using accepts_nested_attributes as follows: class Timesheet < ActiveRecord::Base attr_accessible :status, :user_id, :start_date, :end_date, :activities_attributes has_many :activities, dependent: :destroy has_many :time_entries, through: :activities accepts_nested_attributes_for :activities, allow_destroy: true end class Activity < ActiveRecord::Base attr_accessible :task_id, :timesheet_id, :time_entries_attributes validates :task_id, presence: true, uniqueness: { scope: ''timesheet_id''} belongs_to :timesheet belongs_to :task has_many :time_entries, order: :workdate, dependent: :destroy accepts_nested_attributes_for :time_entries, allow_destroy: true, reject_if: proc { |a| a[:worktime].blank? } end As you see, I didn''t define any validation for timesheet_id presence in Activity model. It works fine in the browser, but it is still possible to create an activity without timesheet_id assigned. If I add: validates :timesheet_id, presence: true to the Activity model, I can''t any more create a timesheet via browser. Any idea ? 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/-/BKWet3UydOsJ. For more options, visit https://groups.google.com/groups/opt_out.
Serguei Cambour
2013-Mar-25 20:06 UTC
Re: validates presence of foreign key fails in nested form
Found the way to do that by using ''inverse_of'' option (see below): On 25 Mar 2013, at 16:03, Javix <s.cambour-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m using accepts_nested_attributes as follows: > > class Timesheet < ActiveRecord::Base > attr_accessible :status, :user_id, :start_date, :end_date, :activities_attributes > has_many :activities, dependent: :destroy > has_many :time_entries, through: :activities, inverse_of: :timesheet > accepts_nested_attributes_for :activities, allow_destroy: true > end > > class Activity < ActiveRecord::Base > attr_accessible :task_id, :timesheet_id, :time_entries_attributes > validates :task_id, presence: true, uniqueness: { scope: ''timesheet_id''} > belongs_to :timesheet, inverse_of: :activities > belongs_to :task > has_many :time_entries, order: :workdate, dependent: :destroy, inverse_of: :activity > > accepts_nested_attributes_for :time_entries, allow_destroy: true, reject_if: proc { |a| a[:worktime].blank? } > end > > As you see, I didn''t define any validation for timesheet_id presence in Activity model. It works fine in the browser, but it is still possible to create an activity without timesheet_id assigned. > > If I add: > > validates :timesheet_id, presence: true > > to the Activity model, I can''t any more create a timesheet via browser. Any idea ? > Regards > > -- > You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/eWvjCoKY4fI/unsubscribe?hl=en-US. > To unsubscribe from this group and all its topics, 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/-/BKWet3UydOsJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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.
Apparently Analagous Threads
- How to use group in nested associations
- accepts_nested_attributes: undefined method 'association'_attributes
- nested forms: use validate :some_method works in update mode only
- fields_for with accepts_nested_attributes: how to pass a value to a label
- Eager loading problem. Help greately appreciated