views:

65

answers:

1

This is killing me... When trying to add a key to like so:

category.site_ids << 1
category.save

It does not save. But when overriding completely, it works:

category.site_ids = [1]
category.save

What am I missing here?

A: 

The simple answer is that rails doesn't support it :) That might sound like a smartmouth answer, but let me explain. First, here's the documentation.

We want to think that category.site_ids is a regular array, but it's not. In order for you to treat it like an array and have all the database magic (records created, updated, destroyed) behind the scenes, the rails developers have to override array methods. Arrays in ruby have several dozen methods, and making collection associations (has many, habtm) do all of them would be impractical.

Now, you can do category.sites << site, and that works. So there's no technical issue with doing the same for id's. It might be a choice, or it might be that nobody has coded it yet :)

Jaime Bellmyer
Thanks, Jaime. I was going over the documentation you linked to yesterday and kept thinking I was missing something... You nailed exactly what I had done previously (<< object) but forgot. Thanks again.
TMB
Glad I could help!
Jaime Bellmyer