How do I order by foreign attribute for belongs_to refernece where there are 2 keys to foreign table
Posted
by Will
on Stack Overflow
See other posts from Stack Overflow
or by Will
Published on 2010-04-12T15:56:35Z
Indexed on
2010/04/12
16:03 UTC
Read the original article
Hit count: 314
I have a Model which has a belongs_to
association with another Model as follows
class Article
belongs_to :author, :class_name => "User"
end
If I wanted to find all articles for a particular genre ordered by author I would do something like the following
articles = Article.all(:includes => [:author], :order => "users.name")
However if Article
happens to have two references to User
how can I sort on :author
?
class Article
belongs_to :editor, :class_name => "User"
belongs_to :author, :class_name => "User"
end
I have tried
articles = Article.all(:includes => [:author], :order => "users.name")
#=> incorrect results
articles = Article.all(:includes => [:author], :order => "authors.name")
#=> Exception Thrown
© Stack Overflow or respective owner