Forum: Ruby on Rails Non Default :primary_key doesnt get updated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ed2b9ee980954d9c0a85619e2b030b57?d=identicon&s=25 sudhakar m (Guest)
on 2008-12-24 09:42
(Received via mailing list)
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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2008-12-24 10:48
(Received via mailing list)
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")
Ed2b9ee980954d9c0a85619e2b030b57?d=identicon&s=25 sudhakar m (Guest)
on 2008-12-24 11:19
(Received via mailing list)
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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2008-12-24 11:27
(Received via mailing list)
what's in your log when you execute these commands?
Ed2b9ee980954d9c0a85619e2b030b57?d=identicon&s=25 sudhakar m (Guest)
on 2008-12-24 11:41
(Received via mailing list)
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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2008-12-24 12:10
(Received via mailing list)
try the following:

  post = Post.new(:title => "This works", :body => "at least for me it
does")
  post.ida2a2 = 234
  post.save
  Post.last
Ed2b9ee980954d9c0a85619e2b030b57?d=identicon&s=25 sudhakar m (Guest)
on 2008-12-24 12:51
(Received via mailing list)
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 <mayer.dominik@gmail.com>
97f7778bd994b7e4c5ad025be0640bde?d=identicon&s=25 Andrius Chamentauskas (Guest)
on 2008-12-25 13:22
(Received via mailing list)
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".
Ed2b9ee980954d9c0a85619e2b030b57?d=identicon&s=25 sudhakar m (Guest)
on 2008-12-26 23:28
(Received via mailing list)
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.
This topic is locked and can not be replied to.