如何在 Ruby on Rails 连接表中加载数据?
我有以下有效的连接表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
前提是您有一个名为 RolesUser 的模型。
如果您有 Habtm 协会,则该模型可能不存在。
加载角色的一种方法是:
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;
据我了解,您的
用户
可以拥有许多角色
,对吧? 如果是这样..那么您可以执行
has_and_belongs_to_many
关联创建一个具有两个 FK 的联接表。 如果您需要连接表中的额外数据,则需要使用has_may :through
而不是has_and_belongs_to_many
。我强烈建议阅读ActiveRecord 关联指南。 祝你好运!
From what I understand, your
user
can have manyroles
, right? If so..Then you can do
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 usehas_may :through
instead ofhas_and_belongs_to_many
.I strongly recommend reading the guide on ActiveRecord associations. Good luck!
你快到了。 表名是模型名的复数形式。 该表名为
RolesUsers
,因此模型名为RolesUser
。另外,我认为如果您要在事后调用
save!
,您会更喜欢使用new
方法。 调用create
会自动保存记录。推荐阅读: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 namedRolesUser
.Also, I think you would prefer to use the
new
method if you're going to callsave!
after the fact. Callingcreate
automatically saves the record.Recommended reading: http://api.rubyonrails.org/classes/ActiveRecord/Base.html
首先,当您有关联 has_and_belongs_to_many 时,您实际上没有模型,您只有具有由参与关联的模型组合而成的复数名称的数据库表,例如,
您没有获得名为 UserRoles、UsersRoles、UsersRole 或任何类型的模型模型的,这仅使您可以在 User 实例或 Role 实例上使用一些方法来查找 1 个用户的所有角色,或者查找具有某个角色 ant 的所有用户等。
这是有效的,rails 将查找数据库表命名roles_users(它查找与两个模型名称以复数形式组合的表,按字母顺序排序,这就是为什么它是roles_users而不是users_roles)。
对于特定用户,您可以添加角色或预定义现有角色,例如:
这样您将在 Roles_users 表中获取包含用户 user_id 的记录,并且对于 Roles_c 集合中的每个角色都会有记录,例如:
其他方法是添加一些角色,
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
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:
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:
Other way is to add some roles,