2

I am trying to create a string array inside my model, where I can push new strings (IDs) into the string array - but it's not pushing the correct values.

Here is how the model looks:

class CreateCompanies < ActiveRecord::Migration
  def change
  create_table :companies do |t|

  t.string :name
  t.string :email
  t.string :users, array: true, default: []
  t.timestamps null: false
  end
 end
end

And here is how I'm adding the new values to the array in the Controller:

c = Company.new
c.users << @user.id
c.save

A value does indeed get added to the array, but it's not the ID but just some random value like this:

users: "--- []\n\u{478A6}"

Been stuck at this for a couple of days now - does anyone know what I'm missing?

UPDATE: Added the code where I create a new Company

 def create
@user = User.new(user_params)
if @user.save

  @user.id = SecureRandom.random_number(999999)
  session[:user_id] = @user.id

  c = Company.new
  c.id = SecureRandom.random_number(999999)
  c.name = @user.company_name
  c.email = "email goes here"
  c.users << @user.id
  c.save

  @user.company_id = c.id
  @user.save

  redirect_to '/dashboard'
else
  redirect_to '/'
end
end

2 Answers 2

4

If you're trying to build a one-to-many relationship, what you should do is add a company_id field to the users table, add belongs_to :company to your app/models/user.rb and add has_many :users to your app/models/company.rb.

Then you'd be able to access the users just like you need.

UPDATE

If you want to create a company with the user you can just go like this.

@user.company = Company.create name: 'Some name'

Or even better to avoid duplication.

@user.company = Company.find_or_create_by name: 'Some name'
Sign up to request clarification or add additional context in comments.

6 Comments

I see! So is it enough to simply add a company_id column to the Users table? And how would I be able to later get all the users who belong to a Company?
You need to fill all the 3 steps I described. Then you'll be able to call company.users and ActiveRecord will do a query and return an ActiveRecord::Relation object wich acts mostly like an array of users.
Aha! Currently I'm creating a new Company in the User class, when a new User is created. (with the code above) How would I then link them?
Thank you! Please see the code in my updated question - could I then set the parameters of the company (such as ID)?
yes, you can just set the company_id (and you don't need to call c.users << @user.id), although I really don't understand why would you use SecureRandom to generate primary keys. and also you might use if @user.valid? instead of if @user.save to avoid saving the same object twice and possibly messing up the database.
|
0

I wonder if here you want to build many to many relationship, the best way to do this is using a many to many table instead of store all ids as an array string in databases;

If you do want to store like this, the best way should be form a string instead of put array directly. For this things like array.join() would be very helpful for you.

If you still insist your way, maybe you should try array.push() instead of <<

4 Comments

The current relationship is a one-to-many; A User can only have one company, but a company can have many Users. When I try using .push() I get an No Method error. Hmm, so I could eg. retrieve the string object from the model, then join it with the latest ID and update it?
If it is one to many there is no need to put columns in Company table. the normal way to do this is put a column named company_id in User table.
Aha! I'm pretty new to this. In the User class; belongs_to :company - and in the Company class; has_many :users. I already have a column called t.string :company_name in the User model, how can I link these together?
It will automatically create relationship for you, no need to modify the database by yourself. You can read more about this by guides.rubyonrails.org/active_record_basics.html .

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.