How do I load data in a Ruby on Rails table?

I have the following connection table that works:

class CreateRolesUsers < ActiveRecord::Migration
  def self.up
    create_table :roles_users,:id => false do |t|
      t.integer :role_id, :null => false
      t.integer :user_id, :null => false
    end    
  end

  def self.down
    drop_table :roles_users
  end
end

      

But I don't know how to load some data (syntax) by default, I am trying:

roleUser = RoleUser.create(:role_id => 2,:user_id => 1)
roleUser.save!

      

It doesn't work, is it RoleUser ... or something else to use? RolesUser ... etc ..

0


a source to share


4 answers


This is assuming you have a model named RolesUser.

If you have a habtm association, the model probably doesn't exist.



One way to load roles could be:

user = User.create :name => 'John'
role = Role.create :name => 'admin'

user.roles << role

      

+2


a source


As I understand it, yours user

can have a lot roles

, right? If so..

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
end

      

Then you can do



user = User.create :name => 'John'
role = Role.create :name => 'admin'
roles_users = RolesUser.create :user => user, :role => role

      

Activation has_and_belongs_to_many

creates a connection table with both FKs. If you need additional data in the join table, you will need to use has_may :through

instead has_and_belongs_to_many

.
I highly recommend reading the ActiveRecord associations guide . Good luck!

+1


a source


You're almost there. The table name is the plural form of the model name. The table is called RolesUsers

, so the model is called RolesUser

.

Also, I think you would rather use a method new

if you are going to call save!

after the fact. The call create

automatically saves the recording.

rolesUser = RolesUser.new(role_id => 2,:user_id => 1)
rolesUser.save!

      

Recommended reading: http://api.rubyonrails.org/classes/ActiveRecord/Base.html

0


a source


First, when you have an association has_and_belongs_to_many, you really don't have a model, you only have a multiple name database table joined with models that participate in the association, for example

class User < ActiveRecord::Base
   has_and_belongs_to_many :roles
end

class Role < ActiveRecord::Base
   has_and_belongs_to_many :users
end

      

You don't get a model named UserRoles or UsersRoles or UsersRole or any model, this only does some methods you can use on a user or role instance instance to find for 1 user all his roles or find all users with some ant role etc. .d.

This works that rails will look for a database table named role_users (it looks for a table that matches the plural names of both models, sorted alphabetically, so its supersedes roles, not users_roles).

For a specific user, you can add roles or predefine existing ones, for example:

# find one user
user = User.first
# get collection of roles
roles_c = Role.where("some conditional statement to find roles")
# set user roles to found collection
user.roles = roles_c
# save changes
user.save

      

Thus, you will get entries in the role_users table with the user_id of the user and for each role in the role_c collection there will be an entry, for example:

# if user.id is 1
# and in roles_c you have 3 roles with id_s 2,5 and 34
# in roles_users table there will be created 3 records with

user_id => 1, role_id => 2
user_id => 1, role_id => 5
user_id => 1, role_id => 34

      

Another way is to add some roles,

user = User.first
roles_c = Role.where('conditional statement')
user.roles << roles_c
user.save

      

0


a source







All Articles