1

Model

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

1
  • 3
    I don't have enough information to help. You should describe in more detail what you are doing. How exactly are you inserting elements one by one? Did you initialize the material.user_ids before appending with <<? What do you mean by "it does not work"? How exactly did you diagnose this? Many things could have gone wrong, so being specific is important. For example, add an excerpt from what you did on the Rails console. Also, please improve your writing. It is riddled with errors. Commented Aug 30, 2016 at 22:16

1 Answer 1

4

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

material.users << user

Read here for other ways of adding associations - http://edgeguides.rubyonrails.org/association_basics.html#has-many-association-reference

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.