检索另一个父级订购的子记录

时间:2013-09-12 22:40:34

标签: ruby-on-rails ruby-on-rails-4

这种关联在Rails 3中运行正常,因此对于用户,我可以通过Quest.status订购UserQuest。我无法在Rails 4中找到如何获得这个。感谢您的帮助。

class Quest < ActiveRecord::Base
  has_many :user_quests
end

class UserQuest < ActiveRecord::Base
  belongs_to :user
  belongs_to :quest
end

class User < ActiveRecord::Base
  has_many :user_quests, :include => :quest, :order => "Quest.status DESC", :dependent => :delete_all
end        

1 个答案:

答案 0 :(得分:0)

试试这个:

class User < ActiveRecord::Base
  has_many :user_quests,
           -> { includes(:quest).order('quests.status DESC') },
           :dependent => :delete_all
end