Marco Song Marco Song - 7 months ago 49
Ruby Question

Rails: can not insert element to array


Materials_Users(Join table)

In console, when I try
material.user_ids = [1,2]
, it works well. But when I try to insert element one by one, like
material.user_ids << 3
, it does not work, the array turn to empty and nothing is inserted into the array.

How to solve this problem?


This is because the shovel method only works for objects. Do this instead.

material.users << user

Read here for other ways of adding associations -