Topic: accessing associated data from models in the form

Hi ,
I want to implement a searchpage for streams by selecting the parameters to be searched from different models.

I have three models called:
containerformat
transport_stream
program

and controllers:
containerformats
transport_streams
programs
streams

each has association like:
in containerformat model:
has_many :transport_streams
belongs_to:programs
belongs_to:streams


in transport_stream model:
has_many :programs
belongs_to:streams

in program model:
has_many :streams

now i want to access the data in the index.html.erb of containerformats from transportstreams and programs .
i found somthing like we need to use 'feilds_for' in the form but i could not able to get the values from other models.

Can any one suggested how to get it work.

thanks
Srikanth

Re: accessing associated data from models in the form

I recommend Ryan Daigle's blog on the new nested forms in Rails 2.3, which I hope you're using because they are so much easier to use now than in earlier versions wink

http://ryandaigle.com/articles/2009/2/1 … attributes

If you're using an older version of rails, check out the Tutorials section of this forum, Ryanb posted a few great tutorials on how to work with more than one object in a form: http://railsforum.com/viewtopic.php?id=719

Re: accessing associated data from models in the form

I hope you don't mind me posting this link everywhere but I've found a lot of people have had the same problem with trying to get nested forms working and I've written a post to solve a commonly missed step :

http://www.pixellatedvisions.com/2009/0 … new-record

Ryan's post mentions a similar way of doing the above but it isn't listed as a step so people naturally assume they've done all the steps and it should work!

Re: accessing associated data from models in the form

Thanks for that! You're right Ryan didn't explain the limitations of has_one relationships vs has_many in his tutorials and it's been a stumbling for a lot of people (including me wink)

Re: accessing associated data from models in the form

Oh right, so is this just something that is needed for has_one relationships then?

Re: accessing associated data from models in the form

I didn't read the post in great detail but yes, the issue is that if you have a has_many relationship a lot of helper methods are available automatically. So even though @user.blogs returns what looks like a simple empty array ([]) it still functions as an ActiveRecord collection underneath. As a result you have access to useful helper methods like @user.blogs.build.

On the other hand if you use a has_one relationship, @user.blog will return a simple Nil object if you haven't populated it yet. A nil object obviously doesn't respond to #build or #valid? or any other ActiveRecord method. The end result is that @user.blog.build returns "no such method #build for nil object." This is why you need a custom builder method as described in your blog which essentially does this:

@user.blog = Blog.new

Last edited by marsvin (2009-03-18 15:33:37)

Re: accessing associated data from models in the form

Ahhhhh right, that makes a lot of sense. I'll update my post to mention it only applies to has_one.

Cheers for that wink