Ruby on Rails
Monday, March 25, 2013
Found the way to do that by using 'inverse_of' option (see below):
On 25 Mar 2013, at 16:03, Javix <s.cambour@gmail.com> wrote:
I'm using accepts_nested_attributes as follows:class Timesheet < ActiveRecord::Baseattr_accessible :status, :user_id, :start_date, :end_date, :activities_attributeshas_many :activities, dependent: :destroyhas_many :time_entries, through: :activities, inverse_of: :timesheetaccepts_nested_attributes_for :activities, allow_destroy: trueendclass Activity < ActiveRecord::Baseattr_accessible :task_id, :timesheet_id, :time_entries_attributesvalidates :task_id, presence: true, uniqueness: { scope: 'timesheet_id'}belongs_to :timesheet, inverse_of: :activitiesbelongs_to :taskhas_many :time_entries, order: :workdate, dependent: :destroy, inverse_of: :activityaccepts_nested_attributes_for :time_entries, allow_destroy: true, reject_if: proc { |a| a[:worktime].blank? }endAs 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: trueto 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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment