Topic: need to insert two records as child of a parent with has_many relation

Hi ,

I have a registeration form in which I need to enter details of two children and a parent.
Lets take a example of mother registering with information of her two children

I allready has done assosiation via has_many.

now the problem statement.

1) in html form if I use form_tag then I need to insert each record-column  manually, is their any better option then

mother.name=params[:name]
mother.email=params[:email]

2) for adding each child I have to again do all manuall stuff.

mother.childrens.create(:name=>params[:child1name]    ,:name=>params[:child1age])
mother.childrens.create(:name=>params[:child2name]    ,:name=>params[:child2age])


is their any better option to do above task??


Smiles,
ManMohan VYas

Re: need to insert two records as child of a parent with has_many relation

If you set up your field names like this:

text_field_tag "mother[name]"
text_field_tag "mother[email_]"
#in the above line, change "email_" to "email".  I had to add the underscore to get past railsforum's stupid text validation.

that will create a structure in params like

params = {:mother => {:name => "susan", :email => "foo@foo.com"}}

you can then say

@mother = Mother.new(params[:mother])

Here you are just passing a hash of fieldnames and values ({:name => "susan", :email => "foo@foo.com"}) to the "new" method, there's nothing magical going on.  It's equivalent to saying

@mother = Mother.new
@mother.name = params[:mother][:name]
@mother.email = params[:mother][:email]

When it comes to the children, you can follow a similar pattern.  Look at this article for an explanation of how to set up your model and form:  http://ryandaigle.com/articles/2009/2/1 … attributes

Last edited by Max Williams (2011-07-11 06:44:25)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: need to insert two records as child of a parent with has_many relation

If use nesting set you can add custom validation of count children to your model

 validates :children_count
 
  private
    def children_count
      error[:children] = 'should be two' unless children.size==2
    end

But instead using nesting set, you can use MVP pattern with active_presenter gem.

In gemfile:

gem 'active_presenter', :git=>'git://github.com/jamesgolick/active_presenter.git', :branch=>'rails3'

Also create, for example, mother_registration.rb and put:

class MotherRegistration < ActivePresenter::Base
  presents :mother=>User, :first_child=>User, :second_child=>User

  before_save :set_relation


  private

    def set_relation
      mother.children << first_child
      mother.children << second_child
    end

end

And that all. Now, if you create MotherRegistration object, it join 3 three objects(mother and children). To set information to mother object type
mother_registration.mother.name or  mother_registration.mother_name.
That rule accept to children too. In the end that object will saved, only if all objects are valid.

Ps sorry for my english

Last edited by jalkoby (2011-07-12 12:48:00)