【发布时间】:2014-05-09 16:36:47
【问题描述】:
我有两个模型 User 和 Business。许多用户可以拥有一个企业,一个用户可以拥有许多企业。
用户也可以是企业或客户的员工。
但只关注所有者业务关联,我在尝试使用用户 ID 时遇到了麻烦,同时将其称为所有者。
我已经设置了一个 BussinessesOwners 连接表并具有以下模型:
class User < ActiveRecord::Base
has_many :businesses, through: :businesses_owners
end
class Business < ActiveRecord::Base
has_many :owners, :class_name => 'User', :foreign_key => "owner_id", through: :businesses_owners
end
class BusinessesOwners < ActiveRecord::Base
belongs_to :users, :foreign_key => "owner_id"
belongs_to :businesses
end
企业所有者迁移:
class CreateBusinessOwners < ActiveRecord::Migration
def change
create_table :business_owners, :id => false do |t|
t.integer :business_id
t.integer :owner_id
end
end
end
如何设置关联以将用户模型称为所有者? - 那么 Businesses.owners 会返回一个用户列表吗?
【问题讨论】:
标签: ruby-on-rails model has-many-through