同一模型上的多个关联
Multiple associations on same models
我有针对企业和用户的模型。一个企业可能有多个经理,但只有其中一个经理可以是所有者。我尝试建模如下:
class Business < ApplicationRecord
has_many :managements
has_many :managers, through: :managements, source: :user, autosave: true
has_one :ownership, -> { where owner: true },
class_name: 'Management',
autosave: true
has_one :owner, through: :ownership, source: :user, autosave: true
end
class Management < ApplicationRecord
belongs_to :user
belongs_to :business
end
class User < ApplicationRecord
has_many :managements
has_many :businesses, through: :managements
end
在我尝试挽救业务之前,这一切似乎都运行良好:
business.owner = owner
#<User id: nil, email: "owner@example.com", password_digest: "a$aXswT85yAiyQ/3Pa2QAMB.4eDs9JPikpFfb8fJtwsUg...", confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, created_at: nil, updated_at: nil>
business.ownership
#<Management id: nil, user_id: nil, business_id: nil, owner: true, created_at: nil, updated_at: nil>
(byebug) business.owner
#<User id: nil, email: "owner@example.com", password_digest: "a$aXswT85yAiyQ/3Pa2QAMB.4eDs9JPikpFfb8fJtwsUg...", confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, created_at: nil, updated_at: nil>
(byebug) business.valid?
false
(byebug) business.errors
#<ActiveModel::Errors:0x007f9ef5ca5148 @base=#<Business id: nil, longitude: #<BigDecimal:7f9ef2c23e48,'0.101E3',9(27)>, latitude: #<BigDecimal:7f9ef2c23d58,'-0.23E2',9(27)>, address: "line 1, line2, town, county", postcode: "B1 1NL", business_name: "Business 1", deleted_at: nil, created_at: nil, updated_at: nil>, @messages={:"ownership.business"=>["must exist"]}, @details={:"ownership.business"=>[{:error=>:blank}]}>
不明白ownership.business
是什么关系?我该怎么做才能完成这项工作?
我正在使用 rails 5.0.6.
尝试将 inverse_of: :business
附加到 has_one :ownership
has_one :ownership, -> { where(owner: true) },
class_name: 'Management',
autosave: true,
inverse_of: :business
从 documentation 看来,:inverse_of
选项似乎是一种避免 SQL 查询的方法,而不是生成它们。这是对 ActiveRecord
使用已经加载的数据而不是通过关系再次获取它的提示。
我认为 :inverse_of
在处理尚未持久化的关联时最有用。
- 如果没有
:inverse_of
参数,ownership.business
将 return 为零,因为它会触发 sql 查询并且数据尚未存储。
- 使用
:inverse_of
参数,从内存中检索数据。
我有针对企业和用户的模型。一个企业可能有多个经理,但只有其中一个经理可以是所有者。我尝试建模如下:
class Business < ApplicationRecord
has_many :managements
has_many :managers, through: :managements, source: :user, autosave: true
has_one :ownership, -> { where owner: true },
class_name: 'Management',
autosave: true
has_one :owner, through: :ownership, source: :user, autosave: true
end
class Management < ApplicationRecord
belongs_to :user
belongs_to :business
end
class User < ApplicationRecord
has_many :managements
has_many :businesses, through: :managements
end
在我尝试挽救业务之前,这一切似乎都运行良好:
business.owner = owner
#<User id: nil, email: "owner@example.com", password_digest: "a$aXswT85yAiyQ/3Pa2QAMB.4eDs9JPikpFfb8fJtwsUg...", confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, created_at: nil, updated_at: nil>
business.ownership
#<Management id: nil, user_id: nil, business_id: nil, owner: true, created_at: nil, updated_at: nil>
(byebug) business.owner
#<User id: nil, email: "owner@example.com", password_digest: "a$aXswT85yAiyQ/3Pa2QAMB.4eDs9JPikpFfb8fJtwsUg...", confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, created_at: nil, updated_at: nil>
(byebug) business.valid?
false
(byebug) business.errors
#<ActiveModel::Errors:0x007f9ef5ca5148 @base=#<Business id: nil, longitude: #<BigDecimal:7f9ef2c23e48,'0.101E3',9(27)>, latitude: #<BigDecimal:7f9ef2c23d58,'-0.23E2',9(27)>, address: "line 1, line2, town, county", postcode: "B1 1NL", business_name: "Business 1", deleted_at: nil, created_at: nil, updated_at: nil>, @messages={:"ownership.business"=>["must exist"]}, @details={:"ownership.business"=>[{:error=>:blank}]}>
不明白ownership.business
是什么关系?我该怎么做才能完成这项工作?
我正在使用 rails 5.0.6.
尝试将 inverse_of: :business
附加到 has_one :ownership
has_one :ownership, -> { where(owner: true) },
class_name: 'Management',
autosave: true,
inverse_of: :business
从 documentation 看来,:inverse_of
选项似乎是一种避免 SQL 查询的方法,而不是生成它们。这是对 ActiveRecord
使用已经加载的数据而不是通过关系再次获取它的提示。
我认为 :inverse_of
在处理尚未持久化的关联时最有用。
- 如果没有
:inverse_of
参数,ownership.business
将 return 为零,因为它会触发 sql 查询并且数据尚未存储。 - 使用
:inverse_of
参数,从内存中检索数据。