Hi,
I have to use non default primary key in one of my projects. If i save the record, the primary key is not getting updated.
For example, consider
post.rb
class Post < ActiveRecord::Base
set_primary_key :ida2a2
end
schema.rb
create_table “posts”, :id => false, :force => true do |t|
t.integer “ida2a2”
t.string “title”
t.text “body”
t.datetime “created_at”
t.datetime “updated_at”
end
In console
p = Post.new(:ida2a2 => 1, :title => “First Post”, :body => “jsust a test i”)
=> #<Post ida2a2: nil, title: “First Post”, body: “jsust a test i”, created_at: nil, updated_at: nil>
p.save
=> true
p
=> #<Post ida2a2: 1, title: “First Post”, body: “jsust a test i”, created_at: “2008-12-24 08:23:53”, updated_at: “2008-12-24 08:23:53”>
?> Post.first
=> #<Post ida2a2: nil, title: “First Post”, body: “jsust a test i”, created_at: “2008-12-24 08:23:53”, updated_at: “2008-12-24 08:23:53”>
After saving, ida2a2 becomes nil.
Tried with different combination’s, but no success. Can somebody help me here.
Just to make sure you understand: t.integer "ida2a2" does not create
primary key. All it does is create integer column name ida2a2. To
create real primary key use t.primary_key "ida2a2".
To create real primary key use t.primary_key “ida2a2”.
Is it different from t.integer “ida2a2”, :primary_key or “ida2a2”, :primary_key => true . I tried both these things but they didnt set ida2a2 as primary key in table. t.primary_key “ida2a2” looks more appropriate.