有很多通过关联使用3个模型抛出无效错误

时间:2017-11-06 15:26:54

标签: ruby-on-rails has-many-through

我有3个模特

链接像素促销

我创建了另一个带有Link,Pixel和Promotion的模型像素化作为参考。

这就是模型的样子

class Link < ApplicationRecord
  belongs_to :user
  has_many :pixelizations
  has_many :pixels, through: :pixelizations, :dependent => :destroy
  has_many :promotions, through: :pixelizations, :dependent => :destroy
end

class Pixel < ApplicationRecord
  belongs_to :user
  has_many :pixelizations
  has_many :links, through: :pixelizations, :dependent => :destroy
end

class Promotion < ApplicationRecord
  belongs_to :user
  has_many :pixelizations
  has_many :links, through: :pixelizations, :dependent => :destroy
end

class Pixelization < ApplicationRecord
  belongs_to :link
  belongs_to :pixel
  belongs_to :promotion
end

class User < ApplicationRecord
has_many :links, dependent: :destroy
         has_many :pixels, dependent: :destroy
         has_many :promotions, dependent: :destroy
end

此外,我正在允许链接控制器中的参数正确

def link_params
      params.require(:link).permit(:user_id, :title, :clicks, :custom_script, :pixel_ids => [], :promotion_ids => [])
    end

现在每当我尝试创建新链接时,我都会收到“无效的像素化”错误。

但是当我创建像素化模型而不添加促销作为参考时,它似乎工作正常。

这是我在新链接页面上遇到的错误“1错误禁止保存此链接: 像素化无效“

这就是我在日志中看到的

Started POST "/links" for 127.0.0.1 at 2017-11-06 19:47:51 +0400
Processing by LinksController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3gSrmxNuaYBd9AaG5cZTskxnS+I9ZS2kuL0W+u0gQbvCuTBYtA07iBufKfvh+cvOqmHWzSkkZkDJDyDiaa
BI1g==", "link"=>{"title"=>"Hello World", "og_url"=>"https://appsumo.com", "promotion_ids"=>["", "", "4"], "custom_script"=>"", "pixe
l_ids"=>["", "", "7"]}, "commit"=>"Pixelize it!"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1

]]
  Pixel Load (0.3ms)  SELECT "pixels".* FROM "pixels" WHERE "pixels"."id" = 7
  Promotion Load (0.3ms)  SELECT "promotions".* FROM "promotions" WHERE "promotions"."id" = 4
  Link Load (0.3ms)  SELECT  "links".* FROM "links" WHERE "links"."short_url" IS NULL LIMIT ?  [["LIMIT", 1]]
   (0.2ms)  begin transaction
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
   (0.1ms)  rollback transaction
  Rendering links/new.html.erb within layouts/application
  Promotion Load (0.4ms)  SELECT "promotions".* FROM "promotions" WHERE "promotions"."user_id" = ?  [["user_id", 2]]
  Pixel Load (0.3ms)  SELECT "pixels".* FROM "pixels" WHERE "pixels"."user_id" = ?  [["user_id", 2]]
  CACHE Pixel Load (0.0ms)  SELECT "pixels".* FROM "pixels" WHERE "pixels"."user_id" = ?  [["user_id", 2]]
  Rendered links/_form.html.erb (11.1ms)
  Rendered links/new.html.erb within layouts/application (12.8ms)
  Rendered shared/_header.html.erb (1.6ms)
  Rendered shared/_notifications.html.erb (0.5ms)
   (0.1ms)  SELECT COUNT(*) FROM "links"
  Link Load (0.2ms)  SELECT "links".* FROM "links"

这里可能出现什么问题?

0 个答案:

没有答案