Rails查询与source,source_tag和多态性的混淆

时间:2018-10-17 02:58:11

标签: ruby-on-rails

我的控制器中有以下查询:

@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.

1 个答案:

答案 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