Hi, I have a topic model which has_many replies. On my topic index page I would like to be able to sort the topics by the number of replies. In my topic model I have defined Topic.rb ... has_many :replies .... def total_replies replies.count end .... And in my controller I have tried @topics = Topic.find(:all, :conditions => ["forum_id = ?", @forum.id], :order => "total_replies DESC") but I get the error Mcolumn "total_replies" does not exist I know the column doesn''t exist, but the question is, how can I get the ''total_replies'' to behave as a column. Thanks, Dan -- 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=en.
I think that''s you could use the counter_cache feature: class AddReplyCount < ActiveRecord::Migration def self.up add_column :topics, :replies_count, :integer, :default => 0 Project.reset_column_information Topic.find(:all).each do |t| t.update_attribute(:replies_count, t.replies.length) end end def self.down remove_column :topics, :replies_count end end class Topic < AR has_many :replies, :order => ''replies_count ASC'' ... end class Reply < AR belongs_to :topic, :counter_cache => true ... end El mié, 21-04-2010 a las 04:10 -0700, DanC escribió:> Hi, > > I have a topic model which has_many replies. > > On my topic index page I would like to be able to sort the topics by > the number of replies. > > In my topic model I have defined > > Topic.rb > ... > has_many :replies > .... > def total_replies > replies.count > end > .... > > And in my controller I have tried > > @topics = Topic.find(:all, :conditions => ["forum_id = ?", > @forum.id], :order => "total_replies DESC") > > but I get the error Mcolumn "total_replies" does not exist > > I know the column doesn''t exist, but the question is, how can I get > the ''total_replies'' to behave as a column. > > Thanks, > > Dan >-- Juan José Vidal Agustín ATICA - Sección de Telemática Universidad de Murcia Tlf: +34 868888742 Fax: +34 868888337 juanjova-mweRjIXS2Z8@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-/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=en.
On 21 April 2010 12:10, DanC <d.m.colegate-VlwAjOFxKoqFxr2TtlUqVg@public.gmane.org> wrote:> @topics = Topic.find(:all, :conditions => ["forum_id = ?", > @forum.id], :order => "total_replies DESC") > > but I get the error Mcolumn "total_replies" does not exist > > I know the column doesn''t exist, but the question is, how can I get > the ''total_replies'' to behave as a column.You can''t - it''s a method on a model, and the DB knows nothing about it. But you can add a column that keeps the value of associated replies and then the query will work. Rails will do this for you with a "counter_cache". http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html (I''m still getting the hijacked domain, so I hope that''s the right link) -- 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=en.
Michael Pavling wrote:> On 21 April 2010 12:10, DanC <d.m.colegate-VlwAjOFxKoqFxr2TtlUqVg@public.gmane.org> wrote: >> @topics = Topic.find(:all, :conditions => ["forum_id = ?", >> @forum.id], :order => "total_replies DESC") >> >> but I get the error Mcolumn "total_replies" does not exist >> >> I know the column doesn''t exist, but the question is, how can I get >> the ''total_replies'' to behave as a column. > > You can''t - it''s a method on a model, and the DB knows nothing about it. > But you can add a column that keeps the value of associated replies > and then the query will work. Rails will do this for you with a > "counter_cache". > > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html > (I''m still getting the hijacked domain, so I hope that''s the right link)You could also select it as an additional column (by getting it from the SQL count() function), although I don''t know how well ActiveRecord would deal with that. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/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=en.