[英]search ActiveRecord through rails association
我有2種型號:用戶和預訂
以及連接用戶和預訂的聯接模型所有權
class Book < ActiveRecord::Base
has_many :ownerships
has_many :users, :through => :ownerships,:uniq=>true
...
end
class User < ActiveRecord::Base
has_many :ownerships
has_many :books, :through => :ownerships
end
class Ownership < ActiveRecord::Base
belongs_to :user
belongs_to :book
end
情況是,當用戶A在我的網站上搜索書籍時,我返回用戶A周圍用戶擁有的相關書籍(例如,它們都在同一所大學中)。
我可以使用rails accion嗎?
謝謝@馬克·古克
我最后要做的是:
scope :same_university,lambda{|q,current_user|
where("title like '%#{q}%'").joins(:sell_infos).where(
"sell_infos.university is '#{current_user.university}'")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.