【发布时间】:2012-07-10 23:44:44
【问题描述】:
我想使用以下模型关联来搜索玩家:
class Player < ActiveRecord::Base
belongs_to :user
has_many :abilities
has_many :sports, :through => :abilities
...
end
class User < ActiveRecord::Base
has_one :player
...
end
class Ability < ActiveRecord::Base
belongs_to :player
belongs_to :sport
has_one :level
...
end
class Sport < ActiveRecord::Base
has_and_belongs_to_many :category_sports
has_many :abilities
has_many :players, :through => :abilities
...
end
class CategorySport < ActiveRecord::Base
has_and_belongs_to_many :sports
end
我有一个表单,用户可以在其中填写两个输入:city 和 sport
城市字段在用户模型中为 (@user.city),运动字段可以在 CategorySport 中为 (@category_sport.name) 或在运动模型中为 (@sport.name)。
现在我有以下在 Player 模型中执行搜索:
def search
self.find(:all,:include => 'user',:conditions => ['users.city LIKE ?', "%#{city}%"])
end
我想知道如何在此查询中添加连接模型(能力)和相关(运动、类别运动),以便也按运动进行查找。因此,不仅可以查找用户城市,还可以查找运动。
【问题讨论】:
标签: sql ruby-on-rails-3 activerecord join