How to write the codes in view?(newbie question)


I have two tables and want to join them.

id genre_name genre_order id genre_id name symbol url order display etc

The code of links_Controller is like below.
def list
@links = Genre.find(:all,:include => :link)

Below is the mysql log.
SELECT AS t0_r0, genres.genre_n ame AS t0_r1, genres.genre_order AS t0_r2, AS t1_r0,
links.genre_ id AS t1_r1, AS t1_r2, links.symbol AS t1_r3,
links.url AS t1_
r4, links.order AS t1_r5, links.display AS t1_r6, links.etc AS
t1_r7 FROM
genres LEFT OUTER JOIN links ON links.genre_id =

But I don’t know the way to show them in the view.

How do I have to write the code in the view(list.rhtml)
to get the contents(name,symbol,url etc) of the joined table?

<% for link in @links %>
<%= link[:genre_name] %>
<- works fine
<%= link[:name] %>
<- just blank
<%= link[:url] %>
<- just blank
<%= link[:order] %>
<- just blank
<% end %>

I suggest you find a Rails tutorial and work through it. It doesn’t
seem like you’ve got the basics yet.

Your controller and view code don’t match up. You’re selecting genres
in the controller and then trying to display a bunch of links in the
view. @links = Link.find(:all, :include => :genre) may be what you
need in the controller.

Also, don’t use [] to get values out of AR objects, use the reader
method (eg: instead of link[:name].


Hi, Jonathan.

Thank you for your reply.

I managed to get the result which I wanted.

I have the agile book,
so I try the tutorial from the begining.

Yosuke Suzuki

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