I''ve been poking around this site and web and cannot find the error of my ways here. I have a HABTM relationship between models User and Site. I''m trying to create a few users in script/console and not having any luck getting it to save. u = User.find(:first) <-- returns an object u.sites <-- returns [ ] as I expect u.sites = [1,2] <-- gives me the following error: ActiveRecord::AssociationTypeMismatch: Site expected, got Fixnum I have checked my migrations and tables and everything seems to be in order. Thanks in advance to anyone that can lend me a hand. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Fri, 14 Mar 2008, T. B. wrote:> > I''ve been poking around this site and web and cannot find the error of > my ways here. I have a HABTM relationship between models User and Site. > I''m trying to create a few users in script/console and not having any > luck getting it to save. > > u = User.find(:first) <-- returns an object > u.sites <-- returns [ ] as I expect > u.sites = [1,2] <-- gives me the following error: > > ActiveRecord::AssociationTypeMismatch: Site expected, got Fixnum > > I have checked my migrations and tables and everything seems to be in > order. Thanks in advance to anyone that can lend me a hand.u.sites is a collection of Site objects, so the system doesn''t know what you mean by trying to insert the numbers 1 and 2 into the collection. As it happens, neither do I :-) What effect do you want to bring about exactly? David -- Upcoming Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS, April 14-17 2008, New York City CORE RAILS, June 24-27 2008, London (Skills Matter) See http://www.rubypal.com for details. Berlin dates coming soon! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> As it happens, neither do I :-) What effect do you want to bringabout> exactly?Thanks for the reply, David. I was just browsing your book looking for some help :) I am trying to create rows in the empty join table sites_users (user_id, site_id). I assumed that once I had the user object, I could just assign sites to him by passing it an array. Does this help or am I digging deeper? -- 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Fri, 14 Mar 2008, T. B. wrote:> > > As it happens, neither do I :-) What effect do you want to bring > about >> exactly? > > Thanks for the reply, David. I was just browsing your book looking for > some help :) > > I am trying to create rows in the empty join table sites_users (user_id, > site_id). I assumed that once I had the user object, I could just assign > sites to him by passing it an array. > > Does this help or am I digging deeper?It helps, but you need to pass actual Site objects in the array. To find and pass in the sites with ids 1 and 2, you would do this: user.sites << Site.find(1,2) David -- Upcoming Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS, April 14-17 2008, New York City CORE RAILS, June 24-27 2008, London (Skills Matter) See http://www.rubypal.com for details. Berlin dates coming soon! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> It helps, but you need to pass actual Site objects in the array. To > find and pass in the sites with ids 1 and 2, you would do this: > > user.sites << Site.find(1,2) >Thanks, David, it worked like a charm! I think I was getting confused by my experiment with checkboxes in a view and expected it to work the same way in console. -Todd -- 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 -~----------~----~----~----~------~----~------~--~---
David A. Black wrote:> It helps, but you need to pass actual Site objects in the array. To > find and pass in the sites with ids 1 and 2, you would do this: > > user.sites << Site.find(1,2)You can also do user.site_ids = [1,2] -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -- On Fri, 14 Mar 2008, Mark Reginald James wrote:> > David A. Black wrote: > >> It helps, but you need to pass actual Site objects in the array. To >> find and pass in the sites with ids 1 and 2, you would do this: >> >> user.sites << Site.find(1,2) > > You can also do > > user.site_ids = [1,2]I was about to say: but that will replace the whole collection... but on rechecking the thread I see that that''s what the OP was trying to do :-) Somewhere along the line I accidentally switched to appending. David -- Upcoming Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS, April 14-17 2008, New York City CORE RAILS, June 24-27 2008, London (Skills Matter) See http://www.rubypal.com for details. Berlin dates coming soon! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David A. Black wrote:> Hi -- > > On Fri, 14 Mar 2008, Mark Reginald James wrote: > >> user.site_ids = [1,2] > I was about to say: but that will replace the whole collection... but > on rechecking the thread I see that that''s what the OP was trying to > do :-) Somewhere along the line I accidentally switched to appending. > > > DavidAha! Okay, thanks to you both for the great replies. More than just using Rails, I''m really trying to master Ruby (and programming), so both recommendations have been very insightful. Thanks to you both! -- 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 -~----------~----~----~----~------~----~------~--~---