我简化了这些模型:
class Game::Champ < ActiveRecord::Base
has_one :contract, :class_name => "Game::ChampTeamContract", :dependent => :destroy
has_one :team, :through => :contract
# Attributes: :avg => integer
end
#
class Game::Team < ActiveRecord::Base
has_many :contracts, :class_name => "Game::ChampTeamContract", :dependent => :destroy
has_many :champs, :through => :contracts
end
#
class Game::ChampTeamContract < ActiveRecord::Base
belongs_to :champ
belongs_to :team
# Attributes: :expired => bool, :under_negotiation => bool
end
#
所以我想在这里做的是找到没有 Game :: ChampTeamContract所有或者有的所有Game :: Champs,但是(不:under_negociation OR是:已过期),按Champ.avg ASC排序
我有点坚持使用两个查询,查询结果并对其进行排序。我希望有一个更好的方式来更多地“铁路”
更新:刚刚添加了一个约束:expired
答案 0 :(得分:1)
我刚用一个超级简单的查询进行测试:
@bars1 = Bar.where(:something => 1)
@bars2 = Bar.where(:something => 2)
@bars = @bars1 + @bars2
不确定它是否正确,但它有效......
答案 1 :(得分:1)
尝试类似:
Game::Champs.
joins("left outer join game_champ_team_contracts on game_champ_team_contracts.champ_id = game_champs.id").
where("game_champ_team_contracts.id is null or (game_champ_team_contracts.state != ? or game_champ_team_contracts.state = ?)", :under_negotiation, :expired).
order("game_champs.avg ASC")
如果保持原样,这是一个非常讨厌的行,所以如果你使用它,它需要整理。使用范围或方法尽可能地将其拆分!