1

我正在为rails中的refinerycms进行扩展,这是我遵循的结构:

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

现在,当我想在我的 Project 类中创建一个 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

项目图像.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

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

4

1 回答 1

2

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

于 2012-06-25T14:35:58.903 回答