Marco Song Marco Song - 1 year ago 98
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?

Answer Source

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

material.users << user

Read here for other ways of adding associations -

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download