Рельсы 5.1.2
Рубин 2.5.3
Я понимаю, что есть несколько способов повлиять на эти отношения, однако, этот вопрос больше о том, почему следующее не работает, а не решает проблему реального мира.
has_many
настройка
class Subscriber < ApplicationRecord
has_many :subscriptions, inverse_of: :subscriber
has_many :promotions, through: :subscriptions, inverse_of: :subscriptions
accepts_nested_attributes_for :subscriptions
accepts_nested_attributes_for :promotions
end
class Subscription < ApplicationRecord
belongs_to :subscriber, inverse_of: :subscriptions
belongs_to :promotion, inverse_of: :subscriptions
end
class Promotion < ApplicationRecord
has_many :subscriptions, inverse_of: :promotion
has_many :subscribers, through: :subscriptions, inverse_of: :subscriptions
accepts_nested_attributes_for :subscriptions
accepts_nested_attributes_for :subscribers
end
В приведенной выше модели Subscriber
, которая настроена на использование has_many
, будут работать следующие отношения:
s = Subscriber.new
s.subscriptions.build
# OR
s.promotions.build
После этого я бы ожидал, что Subscriber
будет вести себя так же, как и has_one
отношения
has_one
настройка
class Subscriber < ApplicationRecord
has_one :subscription, inverse_of: :subscriber
has_one :promotion, through: :subscription, inverse_of: :subscriptions
accepts_nested_attributes_for :subscription
accepts_nested_attributes_for :promotion
end
class Subscription < ApplicationRecord
belongs_to :subscriber, inverse_of: :subscription
belongs_to :promotion, inverse_of: :subscriptions
end
class Promotion < ApplicationRecord
has_many :subscriptions, inverse_of: :promotion
has_many :subscribers, through: :subscriptions, inverse_of: :subscription
accepts_nested_attributes_for :subscriptions
accepts_nested_attributes_for :subscribers
end
Однако попытка построить вложенную promotion
ассоциацию с эквивалентными has_one
методами построения приводит к NoMethodError (undefined method 'build_promotion' for #<Subscriber:0x00007f9042cbd7c8>)
ошибке
s = Subscriber.new
s.build_promotion
Однако это работает:
s = Subscriber.new
s.build_subscription
Я чувствую, что логично ожидать, что вложенные has_one
отношения будут строиться так же, как и has_many
.
Это ошибка или по замыслу?