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..
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 8 Jul 2008, at 11:03, Jai Jai wrote:> > 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...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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---