简体   繁体   English

如何在Ruby on Rails联接表中加载数据?

[英]How to load data in a Ruby on Rails Join Table?

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? 它不起作用,是RoleUser ...还是其他用途? RolesUser...etc. RolesUser ...等

That's provided that you have a model named RolesUser. 前提是您有一个名为RolesUser的模型。

If you have a habtm association, the model is probably not there. 如果您有habtm关联,则该模型可能不存在。

One way of loading roles could be; 一种加载角色的方式可能是:

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

user.roles << role

From what I understand, your user can have many roles , right? 据我了解,您的user可以roles许多roles ,对吗? 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

The has_and_belongs_to_many assoiation creates a join table with both FK. has_and_belongs_to_many关联使用两个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 . 如果在has_may :through表中需要额外的数据,则需要使用has_may :through而不是has_and_belongs_to_many
I strongly recommend reading the guide on ActiveRecord associations . 我强烈建议您阅读有关ActiveRecord关联的指南 Good luck! 祝好运!

First when you have association has_and_belongs_to_many you actualy don't have model, you only have database table with plural name combined from models that are participating in association, for example 首先,当您拥有关联has_and_belongs_to_many时,您实际上没有模型,例如,您只有具有复数名称的数据库表与参与关联的模型组合而成

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 model named UserRoles, or UsersRoles, or UsersRole or any kind of model, this only makes some methods that you can use on User instance or Role instance to find for 1 user all of his roles, or to find all users with some role ant etc. 您不会获得名为UserRoles或UsersRoles或UsersRole的模型或任何类型的模型,这只会使您可以在User实例或Role实例上使用某些方法来为1个用户查找其所有角色,或查找所有用户与一些角色蚂蚁等

This works, that rails will look for database table with name roles_users (it looks for table that is combined with both model names in plural, ordering by alphabetical order, thats why its roles_users and not users_roles). 这行得通,Rails会查找名称为role_users的数据库表(它将查找以复数形式与两个模型名称组合的表,并按字母顺序排序,这就是为什么它的role_users而不是users_roles的原因)。

For particular user you can add roles or predefine existing ones, 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

This way you will get records in roles_users table with user_id of user and for every role in roles_c collection there will be record, for example: 这样,您将在roles_users表中获得带有用户的user_id的记录,对于roles_c集合中的每个角色,都会有记录,例如:

# 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

Other way is to add some roles, 另一种方法是添加一些角色,

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

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 . 该表名为RolesUsers ,因此模型名为RolesUser

Also, I think you would prefer to use the new method if you're going to call save! 另外,如果您要调用save! ,我想您最好使用new方法save! after the fact. 事实之后。 Calling create automatically saves the record. 调用create自动保存记录。

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

Recommended reading: http://api.rubyonrails.org/classes/ActiveRecord/Base.html 推荐阅读: http : //api.rubyonrails.org/classes/ActiveRecord/Base.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM