Non Default :primary_key doesnt get updated


#1

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.

Thanks,
Sudhakar.M


#2

is it the same, if you create a new Post without specifiying the
primary key?

Post.new(
:title => “my_title”,
:body => “my_body”
).save
Post.find_by_title(“my_title”)


#3

Hi MaD,

Tried it but same result.

Post
=> Post(ida2a2: integer, title: string, body: text, published: boolean,
created_at: datetime, updated_at: datetime)

Post.new(:title => “First Post”, :body => “jsust a test i”).save
=> true

Post.first
=> #<Post ida2a2: nil, title: “First Post”, body: “jsust a test i”,
published: nil, created_at: “2008-12-24 10:00:50”, updated_at:
“2008-12-24
10:00:50”>

Hope I am not missing something obvious here. BTW I am using sqllite. I
have
made only 2 changes after generating the model.

1.) :id => false for post table
2.) In post.rb added set_primary_key :ida2a2

But the same issue occurs again & again.

Thanks,
Sudhakar.M


#4

Okie got it.

WARNING: Can’t mass-assign these protected attributes: ida2a2
Post Create (0.0ms) INSERT INTO “posts” (“updated_at”, “title”, “body”,
“id”, “created_at”) VALUES(‘2008-12-24 10:33:56’, ‘First Post’, ‘jsust a
test i’, NULL, ‘2008-12-24 10:33:56’)

Thanks for the lead. But how to assign values in this case.

I guess i need to write a method in post.rb for before_save hook.

Any other suggestion?

Sudhakar.M


#5

try the following:

post = Post.new(:title => “This works”, :body => “at least for me it
does”)
post.ida2a2 = 234
post.save
Post.last


#6

what’s in your log when you execute these commands?


#7

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


#8

Hi MaD,

Yes it worked. Thanks again. I was infact looking at wrong place by
using
attr_writer :ida2a2.

Thanks,
Sudhakar.M

2008/12/24 MaD removed_email_address@domain.invalid


#9

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.