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

发布于 2024-07-19 19:16:20 字数 511 浏览 5 评论 0原文

我有以下有效的连接表:

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

但我不知道如何默认加载一些数据(语法),我尝试:

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

它不起作用,是 RoleUser... 还是其他要使用的东西? 角色用户...等

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此生挚爱伱 2024-07-26 19:16:20

前提是您有一个名为 RolesUser 的模型。

如果您有 Habtm 协会,则该模型可能不存在。

加载角色的一种方法是:

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

user.roles << role

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
北恋 2024-07-26 19:16:20

据我了解,您的用户可以拥有许多角色,对吧? 如果是这样..

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
end

那么您可以执行

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

has_and_belongs_to_many 关联创建一个具有两个 FK 的联接表。 如果您需要连接表中的额外数据,则需要使用 has_may :through 而不是 has_and_belongs_to_many

我强烈建议阅读ActiveRecord 关联指南。 祝你好运!

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 => user, :role => role

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!

哭泣的笑容 2024-07-26 19:16:20

你快到了。 表名是模型名的复数形式。 该表名为 RolesUsers,因此模型名为 RolesUser

另外,我认为如果您要在事后调用 save! ,您会更喜欢使用 new 方法。 调用create会自动保存记录。

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

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

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

烟燃烟灭 2024-07-26 19:16:20

首先,当您有关联 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

您没有获得名为 UserRoles、UsersRoles、UsersRole 或任何类型的模型模型的,这仅使您可以在 User 实例或 Role 实例上使用一些方法来查找 1 个用户的所有角色,或者查找具有某个角色 ant 的所有用户等。

这是有效的,rails 将查找数据库表命名roles_users(它查找与两个模型名称以复数形式组合的表,按字母顺序排序,这就是为什么它是roles_users而不是users_roles)。

对于特定用户,您可以添加角色或预定义现有角色,例如:

# 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

这样您将在 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

其他方法是添加一些角色,

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

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

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.

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).

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:

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文