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 testi") => #<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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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") --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
what''s in your log when you execute these commands? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
try the following: post = Post.new(:title => "This works", :body => "at least for me it does") post.ida2a2 = 234 post.save Post.last --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > try the following: > > post = Post.new(:title => "This works", :body => "at least for me it > does") > post.ida2a2 = 234 > post.save > Post.last > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrius Chamentauskas
2008-Dec-25 12:21 UTC
Re: Non Default :primary_key doesnt get updated
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". --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---