Could someone help me to solve my problem?I''m using named_scope,and there seems to be a little problem... Can I sort a model by another model''s column?For example,I have a "users" table,it has a "value" column.And in the user.rb: has_many :articles in the article.rb: belongs_to :user named_scope :by_user_value,:order => "......" What I want to know is,how can I finish the :order=>"...." parameters to sort the articles by there user''s "value"?Can somebody help me with that?Thanks a lot... --~--~---------~--~----~------------~-------~--~----~ 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 Jul 13, 4:01 am, 刘子嘉 <xxmikl.m...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Could someone help me to solve my problem?I''m using named_scope,and there > seems to be a little problem... > Can I sort a model by another model''s column?For example,I have a "users" > table,it has a "value" column.And in the user.rb: > has_many :articles >You need to joins the users table if you want to order by a column on it. Fred> in the article.rb: > belongs_to :user > named_scope :by_user_value,:order => "......" > > What I want to know is,how can I finish the :order=>"...." parameters to > sort the articles by there user''s "value"?Can somebody help me with > that?Thanks a lot...
Thanks very much. Could you give me some more advice how can I do this?I still don''t know how to join the two tables...thanks again.> You need to joins the users table if you want to order by a column on > it. > > Fred > > > in the article.rb: > > belongs_to :user > > named_scope :by_user_value,:order => "......" > > > What I want to know is,how can I finish the :order=>"...." parameters to > > sort the articles by there user''s "value"?Can somebody help me with > > that?Thanks a lot...
Try this: named_scope :by_user_value, :include => :user, :order => ''users.value'' If you''re not accessing the user association, you could also use :joins instead of :include, but I''m guessing you''ll need to load those records anyways. --Matt Jones On Jul 12, 11:01 pm, 刘子嘉 <xxmikl.m...-Re5JQEeQqe8@public.gmane.orgm> wrote:> Could someone help me to solve my problem?I''m using named_scope,and there > seems to be a little problem... > Can I sort a model by another model''s column?For example,I have a "users" > table,it has a "value" column.And in the user.rb: > has_many :articles > > in the article.rb: > belongs_to :user > named_scope :by_user_value,:order => "......" > > What I want to know is,how can I finish the :order=>"...." parameters to > sort the articles by there user''s "value"?Can somebody help me with > that?Thanks a lot...
It works!Thanks a lot... On Jul 14, 5:15 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try this: > > named_scope :by_user_value, :include => :user, :order => ''users.value'' > > If you''re not accessing the user association, you could also > use :joins instead of :include, but I''m guessing you''ll need to load > those records anyways. > > --Matt Jones > > On Jul 12, 11:01 pm, 刘子嘉 <xxmikl.m...@gmail.com> wrote: > > > Could someone help me to solve my problem?I''m using named_scope,and there > > seems to be a little problem... > > Can I sort a model by another model''s column?For example,I have a "users" > > table,it has a "value" column.And in the user.rb: > > has_many :articles > > > in the article.rb: > > belongs_to :user > > named_scope :by_user_value,:order => "......" > > > What I want to know is,how can I finish the :order=>"...." parameters to > > sort the articles by there user''s "value"?Can somebody help me with > > that?Thanks a lot...