Table join

its my code to display list from two tables where
books.subject_id=subjects.id…

def list
@subject= Subject.new
@books = Book.find(:all)
@subjects = Subject.find(:all)
@bookss=Book.find(:all, :joins=>:subject)
end
how can i write code in list.rhtml to display datas from two tables…
i,ve tried like this…

<% @books.each do |c|%>
<%= c.title%> #column from first table
<%=c.name%> #column from second table…

but ,it displays error…
“undefined method `name’ for #Book:0x4779224” any help pls…

On 8 Jul 2008, at 11:03, Jai Jai wrote:

how can i write code in list.rhtml to display datas from two tables…
i,ve tried like this…

<% @books.each do |c|%>
<%= c.title%> #column from first table
<%=c.name%> #column from second table…

Use an association. if you;ve got

class Book
belongs_to :subject
end

and then

<% @books.each do |c|%>
<%= c.title%>
<%=c.subject.name%>
<% end %>

You don’t need the the :joins option (you may want to use :include)

Fred

yeah…it works …thak you …then if i need to select datas from two
tables using foreign key say for eg: like

"select table1.fieldname,table2.fieldname… from table1 left join
table2 on table2.id=table1.table_id " how can i write code for that???
thank u for your kind reply.

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