【问题标题】:Unknown primary key for table表的未知主键
【发布时间】:2012-06-25 14:32:13
【问题描述】:

我正在为 rails 中的炼油厂做一个扩展,这是我遵循的结构:

项目有很多 project_images Project_images belongs_to Project & Image(炼油厂Image类)

现在,当我想在我的项目类中创建一个新的 ProjectImage 对象时,我总是会收到此错误:

Unknown primary key for table refinery_projects_images in model Refinery::Projects::ProjectImage.

我不需要此表的主键,因为它是一个连接表。这是我的模型和迁移文件的代码:

迁移.rb

class CreateProjectsProjects < ActiveRecord::Migration

  def up
    create_table :refinery_projects do |t|
      t.string :title
      t.text :description
      t.string :investor
      t.string :location
      t.string :area
      t.string :purpose
      t.string :architect
      t.string :users
      t.integer :position
      t.integer :position

      t.timestamps
    end

    add_index :refinery_projects, :id

    create_table :refinery_projects_images, :id => false do |t|
      t.references :image
      t.references :project
      t.integer :position
      t.string :category
      t.string :caption
    end

    add_index :refinery_projects_images, [:image_id, :project_id], :uniq => true

  end

  def down
    if defined?(::Refinery::UserPlugin)
      ::Refinery::UserPlugin.destroy_all({:name => "refinerycms-projects"})
    end

    if defined?(::Refinery::Page)
      ::Refinery::Page.delete_all({:link_url => "/projects/projects"})
    end

    drop_table :refinery_projects
    drop_table :refinery_projects_images

  end

end

项目.rb

module Refinery
  module Projects
    class Project < Refinery::Core::BaseModel
      self.table_name = 'refinery_projects'

      attr_accessible :title, :description, :investor, :location, :area, :purpose, :architect, :users, :position, :position, :images_attributes

      acts_as_indexed :fields => [:title, :description, :investor, :location, :area, :purpose, :architect, :users]

      validates :title, :presence => true, :uniqueness => true

      has_many :project_images
      has_many :images, :through => :project_images, :order => 'position ASC'

      accepts_nested_attributes_for :images, :allow_destroy => false

      def images_attributes=(data)
        ProjectImage.delete_all(:project_id => self.id)

        (0..(data.length-1)).each do |i|
          unless (image_data = data[i.to_s]).nil? or image_data['id'].blank?
            project_image = self.project_images.new(:image_id => image_data['id'].to_i, :position => i)
            # Add caption if supported
            if false
              project_image.caption = image_data['caption']
            end
            self.project_images << project_image
          end
        end
      end

    end
  end
end

ProjectImage.rb

module Refinery
  module Projects
    class ProjectImage < Refinery::Core::BaseModel
      self.table_name = 'refinery_projects_images'

      attr_accessible :image_id, :position

      belongs_to :image, :class_name => 'Refinery::Image'
      belongs_to :project, :class_name => 'Refinery::Projects::Project'
    end
  end
end

有人知道他为什么一直在寻找主键吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activerecord refinerycms


    【解决方案1】:

    Refinery::Core::BaseModel 以某种方式从 ActiveRecord::Base 派生而来。当您使用该类时,您的表格布局需要一个 id。如果您不想要 id,请查看 rails 指南中的 has_and_belongs_to_manyhttp://guides.rubyonrails.org/association_basics.html

    【讨论】:

    • 好的,在文档中也提到了这个“在 has_many :through 和 has_and_belongs_to_many 之间选择”,我使用 has_many through 所以这意味着它也应该工作?事实是我无法从炼油厂访问图像类来添加 has_and_belongs_to_many :projects
    • 你能不能不只是(Ruby 风格)重新打开课程并添加你的关联?
    • 好的,但是如果您对我的问题看起来不错,您会发现 projects_images 表包含的不仅仅是连接属性。关于 Rails 文档,他们建议我使用 has_many through =>“如果您需要验证、回调或连接模型上的额外属性,您应该使用 has_many :through。”就像我的问题中提到的那样,我需要位置、类别和标题属性
    • 没错,但为此您需要一个 ActiveRecord 模型,这反过来又要求一个带有 id 的表。原因是例如 Model.find 没有它就无法工作。
    猜你喜欢
    • 2020-07-31
    • 2013-08-06
    • 2011-07-26
    • 2010-10-14
    • 2013-03-10
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多