-2

A small question about rails console. I am using Ruby 2.6.0, Rails 5.2.3. I created a countries table and a users table.

I use has_and_belongs_to_many :users in the Country model and has_and_belongs_to_many :countries in the User model. I also created country_users table following the guide. I want to make multiple values for country_id in users table. I think it is about adding array into one column.

I already have two records in users table. I want to know how can I use rails c to add more values to country_id column in users table.

Example: Two records I had in users table:

    <User id: 1, email: "[email protected]", created_at: "2019-05-21 12:36:08", updated_at: "2019-05-21 12:49:08",countryid: 1, regionid: 1>
    <User id: 2, email: "[email protected]", created_at: "2019-05-21 12:36:18", updated_at: "2019-05-21 12:49:28",countryid: 2, regionid: 2>

How to add '2' into country_id column for first user via console?

4
  • you mean that in coutryid column you want array of countryid? Commented May 22, 2019 at 8:29
  • If a user can have more than a country, you need a join table. See has many through. Commented May 22, 2019 at 8:32
  • Usually (by default) many-to-many in Rails is implemented via intermediate join table - see guides.rubyonrails.org/… for details. It is theoretically possible to do it via embedding (keeping an array of ids for example), but this way has numerous disadvantages and goes against conventions so it might (and will) be painful to maintain with Rails. Commented May 22, 2019 at 8:36
  • Sorry for my unclear description. I use 'has_and_belongs_to_many :users' in country model and so as user model. I also create 'country_user' table following guide 'guides.rubyonrails.org/association_basics.html' . I want to make multiple values for 'countryid' in user table. Is it possible to use 'rails c'? I think it is about adding array into one column. thanks! Commented May 22, 2019 at 8:40

1 Answer 1

1

You're misunderstanding how the relationships are stored in your database.

If you assign an object a parent_id, in your case a country ID, it can only ever have one of these, meaning it can't be used for 'has_and_belongs_to_many' relationships.

You say you've made a join table which should be called country_users (or user_countries). Each of these records will have a country ID and a user ID, which establishes a link between those two records. If you want to establish a relationship like this in console, try:

country = Country.find(2)
user = User.find(1)
user.countries << country

This will create a new record in your join table, with a User ID of 1 and a country ID of 2, and mean when you call user.countries it will include the one you just assigned.

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

4 Comments

Thank you for clear reply. My understanding is: I misuse parent-child relationship and joint table. if country and user tables are both child tables, I can use a joint table like 'countries_users' to record all their relationships. If I make user table as a parent table and country table as a child table, I cannot use joint table. But I can add "countryid" column to user table(parent table) and add multiple country id as an array in it. Is my understanding correct?
Did not get you propery, you want Array of Contry id in User table?
Yep! I know I need to set column as 'array: true' according to 'stackoverflow.com/questions/20937792/…'. But how to add integer array into table by using rails c?
@landy, you don't need country_id column in users table at all, you only need a joint table here. With it you can query User.first.countries and get all countries for first user

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.