表的未知主键

时间:2012-06-25 14:32:13

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

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

Project有很多project_images Project_images belongs_to Project&图像(炼油厂图像类)

现在,当我想在Project类中创建一个ProjectImage的新对象时,我总是遇到这个错误:

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

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

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

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

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

1 个答案:

答案 0 :(得分:2)

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