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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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_idsis 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 🙂