I have the following relationships: class User < ActiveRecord::Base has_many :pictures end class Picture < ActiveRecord::Base belongs_to :user end And now I hace the following code: @user = User.find params[:id] @picture = @user.pictures.new params[:picture] But for user_id to be saved in my pictures db I have to add this line of code: @picture.user_id = @user.id # should not need this... why? That makes no sense. I though all this time the foreign key was automatically stored. Am I going crazy? Thanks for your input :-). Your Friend, John -- John Kopanas john-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org http://www.kopanas.com http://www.cusec.net http://www.soen.info --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/19/06, John Kopanas <kopanas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > And now I hace the following code: > > @user = User.find params[:id] > @picture = @user.pictures.new params[:picture] > > But for user_id to be saved in my pictures db I have to add this line of > code: > > @picture.user_id = @user.id # should not need this... why? > > That makes no sense. I though all this time the foreign key was > automatically stored. Am I going crazy?@picture = @user.pictures.build(params[:picture]) .new does look awfully nice there, however. jeremy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Are you sure... it works and everything so thanks but according to docs build is protected method and we should use new instead... is this new? On 9/19/06, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> > On 9/19/06, John Kopanas <kopanas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > And now I hace the following code: > > > > @user = User.find params[:id] > > @picture = @user.pictures.new params[:picture] > > > > But for user_id to be saved in my pictures db I have to add this line of > code: > > > > @picture.user_id = @user.id # should not need this... why? > > > > That makes no sense. I though all this time the foreign key was > > automatically stored. Am I going crazy? > > > @picture = @user.pictures.build(params[:picture]) > > .new does look awfully nice there, however. > > jeremy > > >-- John Kopanas john-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org http://www.kopanas.com http://www.cusec.net http://www.soen.info --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/19/06, John Kopanas <kopanas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have the following relationships: > > class User < ActiveRecord::Base > has_many :pictures > end > > class Picture < ActiveRecord::Base > belongs_to :user > end > > > And now I hace the following code: > > @user = User.find params[:id] > @picture = @user.pictures.new params[:picture](Debatable if you need an instance var here, but let''s assume you do) @picture = Picture.new(params[:picture]) @user << @picture And/or what Jeremy posted. (I find that over-golfing the solution a bit hard to read, but that''s me.) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 9/19/06, John Kopanas <kopanas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Are you sure... it works and everything so thanks but according to > docs build is protected method and we should use new instead... is > this new?See the has_many docs. Where are you seeing the docs saying build is protected and new should be used instead? jeremy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You are right! But it does say here that build is protected: http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods/JoinDependency.html#M000539 On 9/19/06, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> > > > On 9/19/06, John Kopanas <kopanas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Are you sure... it works and everything so thanks but according to > > docs build is protected method and we should use new instead... is > > this new? > > > See the has_many docs. Where are you seeing the docs saying build is > protected and new should be used instead? > > jeremy > > > > >-- John Kopanas john-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org http://www.kopanas.com http://www.cusec.net http://www.soen.info --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---