我是ROR的新手,我想了解示波器。在我目前的實現中,我得到所有處理器並在視圖中顯示它。如何使用示波器連接多個表格
class ProcessorsController
def index
@processors = Processor.all
end
end
我要修改這個,所以我可以只在用戶管理處理器。這就是我建立關係的方式。
class Processor
belongs_to :feed
#SCOPES (what I have done so far)
scope :feed, joins(:feed)
scope :groups, joins(:feed => :groups).join(:user).where(:admin => true)
end
class Feed < ActiveRecord::Base
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :groups
scope :admin, where(:admin => true)
end
我能夠做到這一點,我撬
pry(main)> Processor.find(63).feed.groups.first.user.admin?
PS:有人能提供一些好的資源,在那裏我可以學習如何使用範圍,如果關係是複雜的。
非常感謝。是的,我確實閱讀了該文檔,但我沒有注意到使用多個連接。 –
你的回答真的說明了如何在範圍內使用連接。它是一樣的。在調用方法之前,只需要'省略'Model name部分。 – Francisco