【问题标题】:has_many through association dependent error is still referenced in table usershas_many 通过关联依赖的错误仍然在表 users 中被引用
【发布时间】:2018-11-18 05:10:25
【问题描述】:

在开始之前,我应该说我已经检查了 Rails 5.1.: destroy records in "has_many: through" association with restrictionhas_many through association dependent destroy under condition of who called destroy 没有结果。

我的应用包含在拥有一个 EMPRESA 的用户中。 一个 EMPRESA 可能有几个 TAGS 一个 TAG 可能有多个 EMPRESAS(为此我使用了 has_many :through)

我的情况:我收到此屏幕错误:

而且我知道这个错误的根源是因为我试图销毁带有待处理引用的项目。但我无法确定问题。

通过查看服务器控制台,我可以猜测问题涉及 empresa、标记和标记。

涉及的模型

class Empresa < ApplicationRecord

  skip_callback :validate, after: :create
  after_initialize :set_default_plan, :if => :new_record?

  attr_accessor :tag_list

  enum plan: [:noplan, :basic, :plus, :premium]

  belongs_to :user
  belongs_to :category, optional: true
  has_many :promos, dependent: :destroy

  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings

  mount_uploader :logo, LogoUploader
  mount_uploaders :fotos, FotosUploader

  def tag_list
    tags.join(", ")
  end

  def tag_list=(names)
    tag_names = names.split(",").collect {|str| str.strip.downcase}.uniq
    new_or_existing_tags = tag_names.collect {|tag_name| Tag.find_or_create_by(name: tag_name)}

    self.tags = new_or_existing_tags
  end

  def set_default_plan
    self.plan ||= :noplan
  end

end

class Tag < ApplicationRecord
  has_many :empresas, through: :taggings
  has_many :taggings, dependent: :destroy

  def to_s
    name
  end

end

class Tagging < ApplicationRecord
  belongs_to :empresa
  belongs_to :tag
end

class Category < ApplicationRecord
  validates :name, presence: true, length:{ minimum: 3 }, uniqueness: true
  has_many :empresas, dependent: :nullify

end
class User < ApplicationRecord

  enum role: [:user, :editor, :admin, :superadmin]
  after_initialize :set_default_role, :if => :new_record?

  has_one :empresa, dependent: :destroy
  has_many :incidents, dependent: :destroy
  has_many :comments, dependent: :destroy
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable


  def set_default_role
    self.role ||= :user
  end


  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


end

schema.rb(已删除不相关的表)

ActiveRecord::Schema.define(version: 20180531033550) do

  enable_extension "plpgsql"

  create_table "empresas", force: :cascade do |t|
    t.string   "logo"
    t.string   "name"
    t.text     "description"
    t.text     "excerpt"
    t.string   "address"
    t.string   "web"
    t.string   "email"
    t.string   "tel"
    t.string   "video"
    t.json     "fotos"
    t.integer  "plan",        default: 0
    t.float    "mlon"
    t.float    "mlat"
    t.string   "schedule0"
    t.string   "schedule1"
    t.string   "schedule2"
    t.string   "schedule3"
    t.string   "schedule4"
    t.string   "schedule5"
    t.string   "schedule6"
    t.string   "schedule7"
    t.string   "schedule8"
    t.string   "schedule9"
    t.string   "schedule10"
    t.string   "schedule11"
    t.string   "schedule12"
    t.string   "schedule13"
    t.string   "schedule14"
    t.string   "schedule15"
    t.string   "schedule16"
    t.string   "schedule17"
    t.string   "schedule18"
    t.string   "schedule19"
    t.string   "schedule20"
    t.string   "schedule21"
    t.string   "schedule22"
    t.string   "schedule23"
    t.string   "schedule24"
    t.string   "schedule25"
    t.string   "schedule26"
    t.string   "schedule27"
    t.integer  "tag_id"
    t.integer  "offer_id"
    t.integer  "user_id"
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
    t.integer  "category_id"
    t.index ["category_id"], name: "index_empresas_on_category_id", using: :btree
  end


  create_table "taggings", force: :cascade do |t|
    t.integer  "empresa_id"
    t.integer  "tag_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["empresa_id"], name: "index_taggings_on_empresa_id", using: :btree
    t.index ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
  end

  create_table "tags", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.integer  "creditos",               default: 0,  null: false
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.integer  "empresa_id"
    t.integer  "role"
    t.string   "first_name"
    t.string   "last_name"
    t.date     "birthdate"
    t.string   "dni"
    t.string   "phone"
    t.string   "address"
    t.string   "gender"
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["empresa_id"], name: "index_users_on_empresa_id", using: :btree
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
  end

  add_foreign_key "comments", "incidents"
  add_foreign_key "comments", "users"
  add_foreign_key "incidents", "users"
  add_foreign_key "promos", "empresas"
  add_foreign_key "taggings", "empresas"
  add_foreign_key "taggings", "tags"
  add_foreign_key "users", "empresas"
end

【问题讨论】:

  • 我认为也涉及到用户模型。请提供代码
  • 你是对的。现在已添加。对不起
  • 你也可以从更新的db/schema.rb 中添加相关部分
  • db/schema.rb 现已添加
  • 您需要从users 表中删除empresa_id 列。 Empresa belongs_to :useruser_id 列,足以让User has_one :empresa 工作

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


【解决方案1】:

users 中不需要 empresa_id
通过以下方式删除参照完整性和列:

rails g migration remove_constraint_from_users

编辑新创建的迁移文件,在def change块中添加以下内容

def change
  remove_foreign_key :users, :empresas
  remove_column :users, :empresa_id
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2015-06-01
    相关资源
    最近更新 更多