Hi, I am currently a little confused with trying to manipulate a named scope that I currently have. I have two associated models e.g Catalogue, has_many :articles Article: belongs_to :catalogue my named scope looks like this (in Class Article) named scope :finished_articles, { :conditions => [ :finished => true ] } This works fine on it''s own, but I need to trim it to show only the most recent finished article per catalogue (based on created_at field) - currently I may have many finished articles per catalogue, but want to find the latest one. I have tried to use two named scopes, the other looking like this: named scope :limited, { :group => "catalogue_id" } which does only leave one entry per catalogue, but rather than being the most recent entry it is the first created. But I want to gather the most recently created article - one from each catalogue. I just want to be able to reverse this order, but I can''t seem to figure out how to do this (I have a default order already set, but this only seems to affect the scope after it has been rendered..) Also setting the order does not seem to have an effect on how the instances are gathered from the database. Thank you for any suggestions! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
it it is rails 3 I think you could use only, http://www.railway.at/2010/03/09/named-scopes-are-dead/ and Active record querying ?? http://edgeguides.rubyonrails.org/active_record_querying.html On Wed, Sep 29, 2010 at 11:03 AM, Pete <peterhughes0@gmail.com> wrote:> Hi, > > I am currently a little confused with trying to manipulate a named > scope that I currently have. > > I have two associated models e.g > > > Catalogue, > > > has_many :articles > > > Article: > > > belongs_to :catalogue > > > my named scope looks like this (in Class Article) > > > named scope :finished_articles, { :conditions => [ :finished => > true ] } > > > This works fine on it''s own, but I need to trim it to show only the > most recent finished article per catalogue (based on created_at field) > - currently I may have many finished articles per catalogue, but want > to find the latest one. > > > I have tried to use two named scopes, the other looking like this: > > > named scope :limited, { :group => "catalogue_id" } > > > which does only leave one entry per catalogue, but rather than being > the most recent entry it is the first created. But I want to gather > the most recently created article - one from each > catalogue. > > > I just want to be able to reverse this order, but I can''t seem to > figure out how to do this (I have a default order already set, but > this only seems to affect the scope after it has been rendered..) > Also > setting the order does not seem to have an effect on how the > instances > are gathered from the database. > > Thank you for any suggestions! > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com<rubyonrails-core%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > >-- thiagocifani twitter.com/thiagocifani -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Hi Pete, This list is for discussions about the core of Ruby on Rails, such as patches and bug fixes. For general help please ask your question on the rubyonrails-talk mailing list. On 30 September 2010 00:03, Pete <peterhughes0@gmail.com> wrote:> Hi, > > I am currently a little confused with trying to manipulate a named > scope that I currently have. > > I have two associated models e.g > > > Catalogue, > > > has_many :articles > > > Article: > > > belongs_to :catalogue > > > my named scope looks like this (in Class Article) > > > named scope :finished_articles, { :conditions => [ :finished => > true ] } > > > This works fine on it''s own, but I need to trim it to show only the > most recent finished article per catalogue (based on created_at field) > - currently I may have many finished articles per catalogue, but want > to find the latest one. > > > I have tried to use two named scopes, the other looking like this: > > > named scope :limited, { :group => "catalogue_id" } > > > which does only leave one entry per catalogue, but rather than being > the most recent entry it is the first created. But I want to gather > the most recently created article - one from each > catalogue. > > > I just want to be able to reverse this order, but I can''t seem to > figure out how to do this (I have a default order already set, but > this only seems to affect the scope after it has been rendered..) > Also > setting the order does not seem to have an effect on how the > instances > are gathered from the database. > > Thank you for any suggestions! > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com<rubyonrails-core%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.