I am using Rails 2.0.2 with mysql database I have two tables: book and author Table book has 3 columns: id, isbn, and title Table author has 3 columns: id, name, and age In the models I have: class Book < ActiveRecord::Base belongs_to :author end class Author < ActiveRecord::Base has_many :books end I have author_id as the foreign key in the book''s table The user will select ''age'' from a list Then I want to display names of all authors with the selected age, and also the ''title'' from book''s table. In plain SQL, the query will be like this: SELECT name, title FROM author INNER JOIN book ON author.id = book.id WHERE age = "the selected age" How can I write this in Ruby? Thanks Cypray -- 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 -~----------~----~----~----~------~----~------~--~---
I think you can find that answer here: http://railscasts.com/episodes/3-find-through-association On Dec 3, 7:43 pm, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I am using Rails 2.0.2 with mysql > database > > I have two tables: book and author > Table book has 3 columns: id, isbn, and title > Table author has 3 columns: id, name, and age > > In the models I have: > class Book < ActiveRecord::Base > belongs_to :author > end > > class Author < ActiveRecord::Base > has_many :books > end > > I have author_id as the foreign key in the book''s table > > The user will select ''age'' from a list > Then I want to display names of all authors with the selected age, > and also the ''title'' from book''s table. > > In plain SQL, the query will be like this: > > SELECT name, title > FROM author > INNER JOIN book > ON author.id = book.id > WHERE age = "the selected age" > > How can I write this in Ruby? > > Thanks > Cypray > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Also http://guides.rails.info/finders.html ----- Ryan Bigg Freelancer http://frozenplague.net On 04/12/2008, at 3:53 PM, Teedub wrote:> > I think you can find that answer here: > > http://railscasts.com/episodes/3-find-through-association > > On Dec 3, 7:43 pm, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> I am using Rails 2.0.2 with mysql >> database >> >> I have two tables: book and author >> Table book has 3 columns: id, isbn, and title >> Table author has 3 columns: id, name, and age >> >> In the models I have: >> class Book < ActiveRecord::Base >> belongs_to :author >> end >> >> class Author < ActiveRecord::Base >> has_many :books >> end >> >> I have author_id as the foreign key in the book''s table >> >> The user will select ''age'' from a list >> Then I want to display names of all authors with the selected age, >> and also the ''title'' from book''s table. >> >> In plain SQL, the query will be like this: >> >> SELECT name, title >> FROM author >> INNER JOIN book >> ON author.id = book.id >> WHERE age = "the selected age" >> >> How can I write this in Ruby? >> >> Thanks >> Cypray >> -- >> Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Thanks for the info guys. Now I am getting this error coming from the author''s model: "syntax error, unexpected tASSOC, expecting '')''" This is what I have in author''s class class Author < ActiveRecord::Base has_many :books def author_age Author.find :all, (:conditions => ["age = ?", params[:authors]]) end end What I want to do is to display all author''s name base on the selected age. please, What is wrong with my query? Thanks Cypray -- 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 4 Dec 2008, at 14:28, Jay Mark wrote:> > class Author < ActiveRecord::Base > has_many :books > > def author_age > Author.find :all, (:conditions => ["age = ?", params[:authors]]) > end > > end > > What I want to do is to display all author''s name base on the selected > age. > > please, What is wrong with my query? >because hashes are written with {} not () (and in fact you don''t even need the {} here) Fred> Thanks > Cypray > > > > > > > > > > > > > > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Thanks a lot Mr. Fred. Now, I am having trouble with displaying the query result on authors\show.html I am having problems referencing the returned object and getting the two columns displayed. The error is: "You have a nil object when you didn''t expect it!" I have this in my model: class Author < ActiveRecord::Base has_many :books def author_age @results = Author.find :all, :conditions => ["age = ?", params[:authors]] end end I have this on view\authors\show.html <table border="1"> <tr> <td width="20%"><p align="center"><i><b>Author Name</b></i></td> <td width="20%"><p align="center"><i><b>Age</b></i></td> </tr> <% @results.each do |result| %> // I am not sure if this is right! <tr> <td><%=h @result.name %></td> <td><%=h @result.age %></td> </tr> </table> What I want to do is to display the ''name'' and ''age'' that the query returned. Please help! Cypray Frederick Cheung wrote:> On 4 Dec 2008, at 14:28, Jay Mark wrote: >> What I want to do is to display all author''s name base on the selected >> age. >> >> please, What is wrong with my query? >> > because hashes are written with {} not () (and in fact you don''t even > need the {} here) > > Fred-- 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 -~----------~----~----~----~------~----~------~--~---
<% @results.each do |result| %> // I am not sure if this is right! <tr> <td><%=h @result.name %></td> <td><%=h @result.age %></td> </tr> should be <% @results.each do |result| %> // I am not sure if this is right! <tr> <td><%=h result.name %></td> <td><%=h result.age %></td> </tr> <% end %> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I made those changes, but I am still getting this error: "You have a nil object when you didn''t expect it!" It is pointing to the line with the loop: <% @results.each do |result| My model look like this: class Author < ActiveRecord::Base has_many :books def author_age @results = Author.find :all, :conditions => ["age = ?", params[:authors]] end end I have this in view\authors\show.html <tr> <td width="20%"><p align="center"><i><b>Author Name</b></i></td> <td width="20%"><p align="center"><i><b>Age</b></i></td> </tr> <% @results.each do |result| %> // It is flagging this line. <tr> <td><%=h result.name %></td> <td><%=h result.age %></td> </tr> <% end %> Is something wrong with the private method in the model or something else? Please help. Cypray Thorsten Mueller wrote:> <% @results.each do |result| %> // I am not sure if this is right! > > <tr> > > <td><%=h @result.name %></td> > <td><%=h @result.age %></td> > </tr> > > should be > > <% @results.each do |result| %> // I am not sure if this is right! > > <tr> > > <td><%=h result.name %></td> > <td><%=h result.age %></td> > </tr> > <% end %>-- 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 -~----------~----~----~----~------~----~------~--~---
> It is pointing to the line with the loop: > <% @results.each do |result|So for one reason or another @results is nil Most likely your query doesn''t return any records. (btw: you better name that @authors instead of @results for readablity) So what is in params[:authors] ? Where does this param come from and does it contain an age? After all, that''s what you ask Rails to search for in @results = Author.find :all, :conditions => ["age = ?", params [:authors]] So if for example params[:authors] would contain an authors id or name (as the name suggests) then it wouldn''t find anything. You could easily debug this with something like: @results = Author.find :all, :conditions => ["age = ?", params [:authors]] logger.info "RESULT: #{@results.size}" Then you would find the number of found records in development.log (Where btw you could see, which params where sent to your controller) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The problem could be the query because when I search for one record, it works. So, this works fine without the query and the loop. <tr> <td width="20%"><p align="center"><i><b>Author Name</b></i></td> <td width="20%"><p align="center"><i><b>Age</b></i></td> </tr> <tr> <td><%=h @author.name %></td> <td><%=h @author.age %></td> </tr> <% end %> But this returns one ''Name'' for the selected ''age''. That means it is returning the name that is in the same row with the selected age, that is, having same id. In the database, there are several names with same age. So, what I want is a query that will return all names with age value equal to the value of the selected age and not the id of the selected age. In SQL, the query will be: SELECT name, age FROM authors WHERE age = ''the selected age'' // the value, not the id of the selected age. So, I need help with a query that will return all the names with the selected age, and also the proper way to display those names and the age on author\show.html cypray Thorsten Mueller wrote:>> It is pointing to the line with the loop: >> <% @results.each do |result| > > So for one reason or another @results is nil > Most likely your query doesn''t return any records. > (btw: you better name that @authors instead of @results for > readablity) > > So what is in params[:authors] ? > Where does this param come from and does it contain an age? > After all, that''s what you ask Rails to search for in > @results = Author.find :all, :conditions => ["age = ?", params > [:authors]] > > So if for example params[:authors] would contain an authors id or > name (as the name suggests) then it wouldn''t find anything. > > You could easily debug this with something like: > @results = Author.find :all, :conditions => ["age = ?", params > [:authors]] > logger.info "RESULT: #{@results.size}" > Then you would find the number of found records in development.log > (Where btw you could see, which params where sent to your controller)-- 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 Thu, Dec 4, 2008 at 11:28 AM, Jay Mark <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > The problem could be the query because when I search for one record, it > works.> In SQL, the query will be: > SELECT name, age > FROM authors > WHERE age = ''the selected age'' // the value, not the id of the selected > age.So answer the previous question:> Thorsten Mueller wrote:>> So what is in params[:authors] ? >> Where does this param come from and does it contain an age?You can look in your logs (Rails and/or MySQL) to see the query that''s being constructed from your code, if nothing else. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---