Has_many and has_one ... and nested form

Hiya

I am looking at Rails 2.3.2 and how to cleanly do the following:

  • I have model User
  • User has many Plans
  • User has one Active Plan
  • New User form has fields on it to create the Active Parent

I want to create a new User with an Active Plan in one form.

At the moment I have:

class User < ActiveRecord::Base
has_one :active_plan, :class_name => ‘Plan’, :conditions =>
[‘active = ?’, true]
has_many :plans

def active_plan_attributes=(attributes)
plans.build(attributes.merge(:active => true))
end
end

So that in the form I can wrap the active plan fields in:

<% f.fields_for :active_plan, @active_plan do |ap| %>

<% end %>

The @active_plan is being set in the user controller new action atm.

Is this the cleanest approach, or is there something else I can use?
accepts_nested_attributes_for did not seem to suit to set the plan to
being active … but could be missing something on that?

cheers
rich

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs