Posts [ 8 ]

Topic: Including Associations in List action

Howdy Everybody, I'm very new to Rails, though I've had lots of experiencing developing MVC apps with PHP (smarty) and Java (spring/hibernate). I'm still trying to get my head around some of the framework vagaries. I'm having trouble understanding exactly how associations get passed around in the MVC framework. For example, I'm writing an app to manage survey replies. One of the columns in my "replies" table is country_id, indicating a numerical representation of the respondee's country that matches up with "id" in the "countries" table. The countries table contains id, the name of the country and the name of the region of that country. Obviously, many different replies could contain the same country_id. So, for my models:

I believe this would be the correct relationship. The first thing I notice is that if I generate scaffold for Reply, it does not display the field country_id for action "list". Does rails automatically treat fields with "_id" differently?

Secondly, I would like to know what I have to override to display the country name from the countries table and not the country_id from the replies table.

Re: Including Associations in List action

Thanks for the quick response, that was very helpful!

As far as the code you suggested for my view, it seems to me that that would be appropriate for the edit (_form.rhtml) view, correct?

What about the code for my list view template? Obviously I don't want a select box there, I just want it to put in the appropriate foreign key. My current list controller creates a modified content_columns so that it only displays the columns I need. Then in the view (the default scaffold view list.rhtml), it iterates over reply and then content_column to format the table. So, if I am using an associated foreign key:

1) How can I add that foreign key column name to content_columns?2) Is it possible to still iterate through content_columns and have it associate with the correct value, or will I have to manually put in the fields I want instead of iterating over content_columns?

A little more clarification is probably needed. My current list.rhtml:

Re: Including Associations in List action

I typically don't use scaffolding so I have not used content_columns. Someone with more experience might be able to better answer your question. My suggestion would be to manually create your list table, but that's just me

Most code examples are usually pulled out of the air and not tested. Use at your own risk!

Re: Including Associations in List action

What needs to go in my reply_controller to supply the proper country so that I can do the following in my view?

<td><%= reply.country.name %> </td>

or

<td><%= country.name %> </td>

With my current reply_controller (just @replies = Reply.find(:all)), rails complains that country is a nil object in my view. Do I just need to do @countries = Country.find(:all) in my reply_controller?

Re: Including Associations in List action

That should just work. Your reply table does have a country_id column, correct? and it has a non-null value, right?(Refering to the reply.country.name form..... and that is inside your "for reply in @replies do" loop right?)

Perhaps try

@replies = Reply.find :all, :include=>countries

but that should be a possibly useful optimization, without changing user-visible behavior.

perhaps try running your replies = Reply.find :all in the console and inspect the returned array to see what looks wrong.