views:

858

answers:

4

Hello I have the following join 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 by default some data (syntax), I try that:

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

It does not work, is it RoleUser... or something else to use? RolesUser...etc.

Thanks for help !

A: 

You're almost there. The table name is a plural form of the model name. The table is named RolesUsers, so the model is named RolesUser.

Also, I think you would prefer to use the new method if you're going to call save! after the fact. Calling create automatically saves the record.

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

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

Schrockwell
+1  A: 

From what I understand, your user can have many 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 => item, :role => order

The has_and_belongs_to_many assoiation creates a join table with both FK. If you need extra data in the join table, you will need to use has_may :through instead of has_and_belongs_to_many.
I strongly recommend reading the guide on ActiveRecord associations. Good luck!

andi
thanks for your help everybody, but I still have an error when I run migrate:An error has occurred, all later migrations canceled:uninitialized constant CreateRolesUsers::RolesUser== CreateRolesUsers: migrating -- create_table(:roles_users, {:id=>false}) -> 0.0470sI use that: roles_users = RolesUser.new(:role_id => 2,:user_id => 1) roles_users.save!I had already set HBTM relations in Role and User models:class Role < ActiveRecord::Base has_and_belongs_to_many :users endclass User < ActiveRecord::Base has_and_belongs_to_many :rolesendAny other idea?Thanks
Make sure that your migration file is called [version]_create_roles_users.rb
andi
A: 

That's provided that you have a model named RolesUser.

If you have a habtm association, the model is probably not there.

One way of loading roles could be;

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

user.roles << role
Tamer Salama
A: 

thanks for your help everybody, but I still have an error when I run migrate:

An error has occurred, all later migrations canceled: uninitialized constant CreateRolesUsers::RolesUser == CreateRolesUsers: migrating -- create_table(:roles_users, {:id=>false}) -> 0.0470s

I use that:

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

I had already set HBTM relations in Role and User models:

class Role < ActiveRecord::Base 
has_and_belongs_to_many :users 
end 

class User < ActiveRecord::Base 
has_and_belongs_to_many :roles 
end

Any other idea? Thanks

by the way, I have created roles and users like that: role = Role.create(:title => "developer") role.save!
something like this should work: http://pastie.org/475341The name of the migration file is also important. I think that is what is causing your error.
andi