# Saving many-to-many in script/console

**URL:** <https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888>\
**Category:** Rails\
**Created:** [March 14, 2008, 4:21am UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888 "2008-03-14T04:21:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [March 14, 2008, 4:21am UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888/1 "2008-03-14T04:21:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [March 14, 2008, 4:30am UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888/2 "2008-03-14T04:30:43Z")

</div>

Hi –

On Fri, 14 Mar 2008, T. B. wrote:

> 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](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [March 14, 2008, 4:37am UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888/3 "2008-03-14T04:37:56Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [March 14, 2008, 5:04am UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888/4 "2008-03-14T05:04:20Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [March 14, 2008, 4:50am UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888/5 "2008-03-14T04:50:05Z")

</div>

Hi –

On Fri, 14 Mar 2008, T. B. wrote:

> 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](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [March 14, 2008, 5:06am UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888/6 "2008-03-14T05:06:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [March 14, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888/7 "2008-03-14T18:25:37Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Fri, 14 Mar 2008, Mark Reginald J. 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.
> 
> David

Aha! 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!

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [March 14, 2008, 12:53pm UTC](https://www.ruby-forum.com/t/saving-many-to-many-in-script-console/131888/8 "2008-03-14T12:53:56Z")

</div>

Hi –

On Fri, 14 Mar 2008, Mark Reginald J. 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.

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](http://www.rubypal.com) for details. Berlin dates coming soon!
