简体   繁体   English

Rails has_many尽管关联-指定键名

[英]Rails has_many though association - specifying key name

I have 2 models a User and Business . 我有2个模型UserBusiness Many Users can Own a Business and a User can Own many Business. 许多用户可以拥有一家企业,而用户可以拥有许多企业。

A User could also be an Employee of a Business or a Client. 用户也可以是企业或客户的雇员。

But just focusing on the Owner Business association I've been having trouble as I'm trying to use to a User id whilst referring to it as an Owner. 但是仅关注所有者业务协会时,我一直在遇到麻烦,因为在尝试将其称为所有者时要使用用户ID。

I've set up a BussinessesOwners join table and have the following models: 我已经建立了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

BusinessesOwners Migration: 企业主迁移:

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

How can I set up the association to refer to the User model as an Owner? 如何设置关联以将用户模型称为所有者? - So Businesses.owners will return a list of users? -那么Businesses.owners将返回用户列表?

Personally, I like to name associations based on the associated tables, in other words: user_id instead of owner_id . 就个人而言,我喜欢根据关联的表来命名关联,换句话说: user_id而不是owner_id And since you are not doing a HABTM relation, you're not bound to the "buisinesses_owners" convention and you can give the through-model a better name such as BusinessOwnership or even Ownership (eg if used polymorphically for any ownership relation between User and another model). 而且,由于您没有进行HABTM关系,因此您不受“ buisinesses_owners”约定的约束,并且可以给直通模型指定一个更好的名称,例如BusinessOwnership甚至Ownership (例如,如果多态地用于User和User之间的任何所有权关系,另一个模型)。

Please note that the belongs_to in the through-model must be singular. 请注意,直通模型中的belongs_to必须为单数。 (Read the association out loud and you'll hear it doesn't make sense to use plural here.) (大声朗读关联,您会听到在这里使用复数没有意义。)

The following should thus work: 因此,以下应该起作用:

class User < ActiveRecord::Base
  has_many :businesses, through: :business_ownerships
  has_many :business_ownerships
end

class Business < ActiveRecord::Base
  has_many :owners,  through: :business_ownerships, source: :user
  has_many :business_ownerships
end

class BusinessOwnership < ActiveRecord::Base
  belongs_to :user
  belongs_to :business
end

And here are the migrations: 以下是迁移:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
    end
  end
end

class CreateBusinesses < ActiveRecord::Migration
  def change
    create_table :businesses do |t|
      t.string :name
    end
  end
end

class CreateBusinessOwnerships < ActiveRecord::Migration
  def change
    create_table :business_ownerships do |t|
      t.references :user
      t.references :business
    end
  end
end

Please note: Unless you add extra attributes to BusinessOwnership or recycle it as a polymorphic Ownership model, it's not really necessary to do a "has_many through" here, you could just as well do to HABTM relations with join tables named according to the corresponding convention. 请注意:除非您向BusinessOwnership添加额外的属性或将其作为多态Ownership模型回收,否则实际上并没有必要进行“ has_many through”操作,您也可以使用根据相应约定命名的连接表来处理HABTM关系。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM