I have 3 models Skill, User, UserSkill User has_many skills :through => :user_skills User has_many :user_skills UserSkill.new # => { :id => nil, :user_id => nil, :skill_id => nil } us = UserSkill.create(:user_id => 1, :skill_id => 1) # => { :id => nil, :user_id => 1, :skill_id => 1 } Why id is nil? In the database record was created and id is not null... -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ps. rails 3.0.3 -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 30 November 2010 10:14, Yan Bernatsky <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have 3 models > Skill, User, UserSkill > > User has_many skills :through => :user_skills > User has_many :user_skills > > UserSkill.new # => { :id => nil, :user_id => nil, :skill_id => nil } > us = UserSkill.create(:user_id => 1, :skill_id => 1) # => { :id => nil, > :user_id => 1, :skill_id => 1 } > > Why id is nil? > In the database record was created and id is not null...The id is not allocated until the record is saved to the database. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #965064:> On 30 November 2010 10:14, Yan Bernatsky <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Why id is nil? >> In the database record was created and id is not null... > > The id is not allocated until the record is saved to the database. > > ColinBut other models returns the id on ".create()" -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> UserSkill.new # => { :id => nil, :user_id => nil, :skill_id => nil } > us = UserSkill.create(:user_id => 1, :skill_id => 1) # => { :id => nil, > :user_id => 1, :skill_id => 1 } > > Why id is nil? > In the database record was created and id is not null...Hi, Friend when you create any "new" object usign UserSkill or any class class which is associated with ActiveRecords.. then it auto create one hash with all attributes of user_skills(or any relevant) table.. this is due to ORM feature of the Rails.. Second thing, that all values seems to nil in that hash.. this is because it provide you blank hash.. to do process or to assign required values to the attributes.. nil is just because as we know ruby has greate feature of NilClass to avoid exceptions on nil values.. I dont know is this answer of your question or not. Good luck ;) -Ganesh K. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > But other models returns the id on ".create()"yes.. in this case method(".create()") first creates record in db table and then it provides you hash with table''s all attributes and values which are there in db records. -Ganesh K -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Model.create(:attr => "value") returns the "#<Model id: 1, attr: Value>" if model is valid and no database''s errors found. It is normal, but my UserSkill returns "#<UserSkill id: nil, etc.. >" on create method. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.