Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set_primary_key error in Rails

I'm creating a table called Index. Here's the migration:

class CreateIndices < ActiveRecord::Migration
  def change
    create_table :indices, {:id => false} do |t|
      t.string :name
      t.float :returns, array: true, default: []
      t.float :navs, array: true, default: []
      t.float :sharpe

      t.timestamps
    end

    execute "ALTER TABLE indices ADD PRIMARY KEY (name);"
  end
end

That all works fine. I saw in another Stack Overflow question that I have to include the set_primary_key command in my model to get it to work, so I have the following in the index.rb

class Index < ActiveRecord::Base
  set_primary_key :name

end

Besides these two files, I haven't changed anything from the default Rails scaffolding (the app was created with Postgres as the default database). When I go to localhost:3000/indices, I get the following error

undefined method `set_primary_key' for #<Class:0x37132e0>

If I comment out the set_primary_key line it loads the regular empty scaffold, but I assume this does not give me the primary key functionality that I want. What am I doing wrong?

like image 704
jhlu87 Avatar asked Dec 04 '13 21:12

jhlu87


1 Answers

If you're using rails 3.2 or higher, the set_primary_key method was depreciated, check out the Rails 3.2 release notes - Section 8.1 and it suggests using an assignment method instead like self.primary_key=, just like you said you did in your comment

like image 64
strivedi183 Avatar answered Nov 05 '22 18:11

strivedi183