Ruby on rails 用他的关联表定义一篇文章的范围
我想知道我们如何利用他的关联表来确定帖子的范围? 我解释我想做什么,它会更清楚。我实际上有一个脚手架:牧场和一个脚手架:员工。当我创建一个新员工时,我可以选择将其与许多牧场相关联。所以我有另外一张表格可以参考这个协会:ranch_staffRuby on rails 用他的关联表定义一篇文章的范围,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想知道我们如何利用他的关联表来确定帖子的范围? 我解释我想做什么,它会更清楚。我实际上有一个脚手架:牧场和一个脚手架:员工。当我创建一个新员工时,我可以选择将其与许多牧场相关联。所以我有另外一张表格可以参考这个协会:ranch_staff 因此,如果我想将职员的范围限定为只显示与实际@ranch相关的内容,我该怎么做呢?类似的方法应该可以做到: 迁移: class Init < ActiveRecord::Migration def change create_table :
因此,如果我想将职员的范围限定为只显示与实际@ranch相关的内容,我该怎么做呢?类似的方法应该可以做到: 迁移:
class Init < ActiveRecord::Migration
def change
create_table :ranches do |t|
t.string "name"
end
create_table :staffs do |t|
t.string "name"
end
create_table :ranches_staffs, id: false do |t|
t.belongs_to :ranch
t.belongs_to :staff
end
end
end
app/models/ranch.rb
:
class Staff < ActiveRecord::Base
has_and_belongs_to_many :ranches
end
class Ranch < ActiveRecord::Base
has_and_belongs_to_many :staffs
end
请参阅。类似的内容应该可以实现以下目的: 迁移:
class Init < ActiveRecord::Migration
def change
create_table :ranches do |t|
t.string "name"
end
create_table :staffs do |t|
t.string "name"
end
create_table :ranches_staffs, id: false do |t|
t.belongs_to :ranch
t.belongs_to :staff
end
end
end
app/models/ranch.rb
:
class Staff < ActiveRecord::Base
has_and_belongs_to_many :ranches
end
class Ranch < ActiveRecord::Base
has_and_belongs_to_many :staffs
end
看