【问题标题】:Many to many join table not updated in Active admin多对多连接表未在 Active admin 中更新
【发布时间】:2017-04-08 14:58:05
【问题描述】:

我有两个模型 Destination 和 Package。一个包可能有多个目的地,目的地可能包含多个包。为了维护关系,我有一个名为 packages_in_destinations 的表。当我尝试在活动管理员中从包表单中插入目标时,它没有显示任何错误,但 packages_in_destinations 表未更新。我正在使用 MySQL 作为数据库。

# app/admin/package.rb
ActiveAdmin.register Package do
  permit_params :package_id, :package_title, :description, :featured_image, :duration, :meta_description, :meta_keywords, :published, :package_categories_id, :destinations_id

  form do |f|
    f.semantic_errors *f.object.errors.keys
      f.inputs "Package Details" do
        f.input :destination_id, :as => :check_boxes, :collection => Destination.all.collect {|destination| [destination.destination_title, destination.id]}
        end
    f.actions
  end
end

# app/models/package.rb file
class Package < ApplicationRecord
  validates_uniqueness_of :package_title, scope: :package_title
  validates :package_title, :description, :package_categories_id, :presence => true
  belongs_to :package_category
  has_and_belongs_to_many :packages_in_destinations
  has_many :destinations, through: :packages_in_destinations
end

# app/models/destination.rb file
class Destination < ApplicationRecord
  validates_uniqueness_of :destination_title, scope: :destination_title
  validates :destination_title, :description, :altitude, :geolocation, :presence=>true
  has_and_belongs_to_many :packages_in_destinations
  has_many :packages, through: :packages_in_destinations
end

# app/models/packages_in_destination.rb
class PackagesInDestination < ApplicationRecord
  has_and_belongs_to_many :destinations, foreign_key: :destination_id, class_name: 'Destination'
  has_and_belongs_to_many :packages, foreign_key: :package_id, class_name: 'Package'
end

已经从迁移文件中创建了两个表之间的关系

class CreatePackagesInDestinations < ActiveRecord::Migration[5.0]
  def up
    create_join_table :packages, :destinations, table_name: :packages_in_destinations do |t|
    t.index :package_id
    t.index :destination_id
    t.timestamps
  end

  def down
    drop_table :packages_in_destinations
  end
end

创建另一个迁移文件以向表中添加主键

class AddingPrimaryInPackaesInDestination < ActiveRecord::Migration[5.0]
  def change
    add_column :packages_in_destinations, :id, :primary_key
  end
end

所以从所有这些事情来看,没有显示错误,但包保存在包表中,但没有在packages_in_destinations 表中插入关系。请有人提出这些遗漏和问题的建议?

【问题讨论】:

    标签: mysql ruby-on-rails ruby activeadmin ruby-on-rails-5


    【解决方案1】:

    您需要使用has_and_belongs_to_manyhas_many :through。不要同时使用两者。

    1) 以has_and_belongs_to_many 为例。在这种情况下,您可以删除PackagesInDestination 模型:

    # app/models/package.rb file
    class Package < ApplicationRecord
      # ...
      has_and_belongs_to_many :destinations, join_table: :packages_in_destinations
    end
    
    # app/models/destination.rb file
    class Destination < ApplicationRecord
      # ...
      has_and_belongs_to_many :packages, join_table: :packages_in_destinations
    end
    
    # app/models/packages_in_destination.rb
    # removed
    

    2) 以has_many :through 为例:

    # app/models/package.rb file
    class Package < ApplicationRecord
      # ...
      has_many :packages_in_destinations
      has_many :destinations, through: :packages_in_destinations
    end
    
    # app/models/destination.rb file
    class Destination < ApplicationRecord
      # ...
      has_many :packages_in_destinations
      has_many :packages, through: :packages_in_destinations
    end
    
    # app/models/packages_in_destination.rb
    class PackagesInDestination < ApplicationRecord
      belongs_to :destination
      belongs_to :package
    end
    

    【讨论】:

    • @SudeepGhimiray 欢迎您。如果我的回答对您有帮助,请考虑通过单击答案左侧的复选标记将其标记为已接受
    猜你喜欢
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    相关资源
    最近更新 更多