我的控制器中有以下查询:
@current_checkout_location.current_submissions.pluck(:id)
这是什么?我必须根据current_checkout_location
创建什么才能使current_submissions
出现?
这是checkout_location
模型的关联:
has_many :current_submissions, through: :tours
这是旅游模型:
has_many :current_catalog_items, class_name: 'CatalogItem', foreign_key: :current_tour_id
has_many :current_submissions, through: :current_catalog_items, source: :catalogable, source_type: 'Submission'
这是怎么回事?我们是否将current_submissions
委派给CatalogItem
模型?我应该期望CatalogItem
模型具有catalogable
方法(由于source
行上的has_many :current_submissions
吗?)
基本上,我正在尝试为@current_checkout_location
创建一些虚假数据,但对于创建内容有些困惑)
这不起作用:
CheckoutLocation.last.current_submissions << [Submission.find(8), Submission.find(7)]
CACHE (0.0ms) SELECT `checkout_locations`.* FROM `checkout_locations` ORDER BY `checkout_locations`.`id` DESC LIMIT 1
Submission Load (1.0ms) SELECT `submissions`.* FROM `submissions` WHERE (purchase_status = "Complete" or purchase_status = "Purchased") AND `submissions`.`id` = 8 LIMIT 1
Submission Load (4.5ms) SELECT `submissions`.* FROM `submissions` WHERE (purchase_status = "Complete" or purchase_status = "Purchased") AND `submissions`.`id` = 7 LIMIT 1
(0.4ms) BEGIN
(0.3ms) ROLLBACK
ActiveRecord::HasManyThroughNestedAssociationsAreReadonly: Cannot modify association 'CheckoutLocation#current_submissions' because it goes through more than one other association.
答案 0 :(得分:0)
ActiveRecord :: HasManyThroughNestedAssociationsAreReadonly:无法 修改关联“ CheckoutLocation#current_submissions”,因为它 经历了不止一个的关联。
在has_many :current_submissions, through: :tours in checkout_location
模型中,您有has_many :current_submissions, through: :current_catalog_items, source: :catalogable, source_type: 'Submission'
,也有Tour
,该模型会在关系(源反射)问题中创建循环引用。从has_many :current_submissions, through: :tours
中删除current_location
。这应该可以解决您的问题。
#current_location.rb
has_many :tours