In my app, a Post belongs_to a user and a User has_many posts. Say I''d like to change the user the post belongs to. post = Post.find(1) post.user << anotheruser ... gives me an error, since post.user is already defined. It also seems as if changing the attribute user_id in this manner: post.update_attributes(:user_id => 1) doesn''t help either. What''s the best way to do this? -- 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-/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 -~----------~----~----~----~------~----~------~--~---
PLease provide more code to get help. Thanks. I don''t understand why do you need add a another user to post. If you want this, then the association is has_and_belongs_to_many or has_many :through between the model users and posts. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Nicolas Santa wrote:> PLease provide more code to get help. Thanks. I don''t understand why do > you need add a another user to post. If you want this, then the > association is has_and_belongs_to_many or has_many :through between the > model users and posts.Sorry, what I mean is that I''d like to _change_ which user the post belongs to, not add other ones. Here''s more code: class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end in the db schema: create_table "posts", :force => true do |t| t.column "user_id", :integer end Thanks in advance for any help. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Read the documentation for belongs_to at api.rubyonrails.com Hint: you''re not pushing a value onto a collection, you are setting a value on at attribute. ReinH On Sep 7, 5:03 am, "Mariko C." <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Nicolas Santa wrote: > > PLease provide more code to get help. Thanks. I don''t understand why do > > you need add a another user to post. If you want this, then the > > association is has_and_belongs_to_many or has_many :through between the > > model users and posts. > > Sorry, what I mean is that I''d like to _change_ which user the post > belongs to, not add other ones. Here''s more code: > > class User < ActiveRecord::Base > has_many :posts > end > > class Post < ActiveRecord::Base > belongs_to :user > end > > in the db schema: > > create_table "posts", :force => true do |t| > t.column "user_id", :integer > end > > Thanks in advance for any help. > -- > Posted viahttp://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-/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 -~----------~----~----~----~------~----~------~--~---