# Has\_many and has\_one ... and nested form

**URL:** <https://www.ruby-forum.com/t/has-many-and-has-one-and-nested-form/172152>\
**Category:** Rails\
**Created:** [July 15, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/has-many-and-has-one-and-nested-form/172152 "2009-07-15T17:58:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![worthee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b38774/32.png) [@worthee](https://www.ruby-forum.com/u/worthee)\
**Post date:** [July 15, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/has-many-and-has-one-and-nested-form/172152/1 "2009-07-15T17:58:31Z")

</div>

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
