BlueHandTalking
2009-Nov-22 21:20 UTC
Defining self method and results for find( :all, :select => "DISTINCT()
A first attempt to denine a self method: In my ProjectItem model I have a field ''book'' In that model there is this method: def self.find_books find( :all, :select => "DISTINCT(book)", :order => "book ASC" ) end In another controller: @books = ProjectItem.find_books Then in a _books.html.erb partial: <% if @books%> <% @books.each do |name| %> <%= link_to ( #{name.book}, {:controller => "projects", :action => "show","} %> <% end %> <% end %> I am expecting that the selection creates an array where where @books[0][:name] is the name of a book. Not so, evidently---or my Query is not correct? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=.
Rick DeNatale
2009-Nov-22 21:55 UTC
Re: Defining self method and results for find( :all, :select => "DISTINCT()
On Sun, Nov 22, 2009 at 4:20 PM, BlueHandTalking <jet-ppry1Xks6u1BDgjK7y7TUQ@public.gmane.org> wrote:> A first attempt to denine a self method: > > In my ProjectItem model I have a field ''book'' > > In that model there is this method: > > def self.find_books > find( :all, :select => "DISTINCT(book)", :order => "book ASC" ) > end > > In another controller: > > @books = ProjectItem.find_books > > Then in a _books.html.erb partial: > > <% if @books%> > <% -5z5bOP24jHByDzI6CaY1VQ@public.gmane.org do |name| %> > <%= link_to ( #{name.book}, > {:controller => "projects", :action => "show","} > %> > <% end %> > <% end %> > > I am expecting that the selection creates an array where > where @books[0][:name] is the name of a book. >@books is a collection of books, not names so <% @books.each do |book| %> <%= link_to ( #{book.name}, {:controller => "projects", :action => "show"} %> <% end %> But this raises other questions. First, why do you want to show a project when the user clicks on the book name, I''d expect you to show the book. Second, why are you asking the ProjectItem class to find ALL of the books? You haven''t said anything about the associations between project items and books, I''m guessing it''s either ProjectItem has_many :books and Book belongs_to :project_item If a book can''t be shared between project items, or ProjectItem has_and_belongs_to_many :books Book has_and_belongs_to_many :project_items if it can, and in the second case you might(should) use an explicit join model but for now lets assume that Books can''t be shared between project items. Given that I''d do something like Book belongs_to :project_item named_scope :sorted_by_name, :order => "books.name ASC" ProjectItem has_many :books And then when you want the books associated with a particular project item, you can use @project_item.books.sorted_by_name HTH -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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=.
Rick DeNatale
2009-Nov-22 21:56 UTC
Re: Defining self method and results for find( :all, :select => "DISTINCT()
On Sun, Nov 22, 2009 at 4:55 PM, Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Nov 22, 2009 at 4:20 PM, BlueHandTalking <jet-ppry1Xks6u1BDgjK7y7TUQ@public.gmane.org> wrote:> Given that I''d do something like > > Book > belongs_to :project_item > named_scope :sorted_by_name, :order => "books.name ASC" > > ProjectItem > has_many :books > > And then when you want the books associated with a particular project > item, you can use > > @project_item.books.sorted_by_name >And by the way if you really want to get all the books sorted by name, you should ask the Book class, not the ProjectItem class, as in Book.all.sorted_by_name -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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=.
BlueHandTalking
2009-Nov-23 01:58 UTC
Re: Defining self method and results for find( :all, :select => "DISTINCT()
Oh, I think you are misled because my syntax is perhaps wrong, but I did mention in my original question that ''book'' is a field of ProjectItem. There is no ''Book'' model. So, in class ProjectItem I have a field ''book''. Each ProjectItem record will have a book field. Many books will have an identical name. I wish to make a list that is comprised of book names, of which each member of the list is a unique example of that book name. So there is no belongs_to or has_many relationship going on. I want this list of unique examples of book names to be sorted in ascending order. When I am creating this instance: @books = ProjectItem.find_books It would actually be a @project_items list, which to my understanding should only consist of @project_items.book data, since I believe :select => "DISTINCT(book)" only returns the data from that particular field (book) and not all the other data. 1)That is what I understand. If I am wrong on this, would like to know where. 2) I thought when I use ''name'' in the following: @books.each do |name| link_to ( #{name.book} ) ...that ''name'' is a symbol only, and just used as a placeholder which is just substituted for each member of the array. If that is correct, then the above would just iterate through for each project_item.book and output a link to it. And @books is a var which actually is an instance of @project_item. Part of reason for this posting is to see if my assumptions are correct and I do have freedom to name instance to something different than the standard approach. Perhaps using a standard naming approach would have increased the clarity. Jet On Nov 22, 1:56 pm, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Nov 22, 2009 at 4:55 PM, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On Sun, Nov 22, 2009 at 4:20 PM, BlueHandTalking <j...-ppry1Xks6u1BDgjK7y7TUQ@public.gmane.org> wrote: > > Given that I''d do something like > > > Book > > belongs_to :project_item > > named_scope :sorted_by_name, :order => "books.name ASC" > > > ProjectItem > > has_many :books > > > And then when you want the books associated with a particular project > > item, you can use > > > @project_item.books.sorted_by_name > > And by the way if you really want to get all the books sorted by name, > you should ask the Book class, not the ProjectItem class, as in > > Book.all.sorted_by_name > > -- > Rick DeNatale > > Blog:http://talklikeaduck.denhaven2.com/ > Twitter:http://twitter.com/RickDeNatale > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn:http://www.linkedin.com/in/rickdenatale-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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=.
BlueHandTalking
2009-Nov-23 06:22 UTC
Re: Defining self method and results for find( :all, :select => "DISTINCT()
I found my error, it was syntax. I used: <% @books.each do |name| %> <% link_to ( #{name.book} ) %> <% end %> It should have been: <% @books.each do |name| %> <% link_to ( name.book) %> <% end %> Cheers, Jet -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=.