从关联中获取数据
Get the data from association
我有两个表用户和组。
user group
----- -------
id id
name group_name
created_by
在我使用的用户模型中,has_and_belongs_to_many :groups, dependent: :destroy
在我使用的组模型中,has_and_belongs_to_many :users, dependent: :destroy
我已经创建了迁移
class UserGameGroup < ActiveRecord::Migration
def change
create_table :user_game_group, id: false do |t|
t.belongs_to :user, index: true
t.belongs_to :group, index: true
end
end
end
所以在我的组控制器的显示方法中,我想获取特定组的用户。
假设如果我目前在第 4 组,我想获取基于该组的所有用户。
我可以做到这一点 Group.where(group_id: 4)
但它只会给我用户的 ID。有没有办法也获取用户名?
在您当前的示例中,您查询的是一个包含方法 users
的组。因此,同样地,您可以使用此调用来检索用户记录的集合。
group = Group.where(group_id: 4)
group.users # Returns a collection of users.
如果你想进行单个查询,你可以使用 ActiveRecord::QueryMethods
include
这样的方法。
Group.includes(:user).where(group_id: 4)
您应该将加入的群组重命名为 groups_users
。 Rails 希望加入的组采用这种格式(加入的 table 的小字母在前,table 的名称由 _
分隔)。 plus both 应该是复数。此外,您的 table 名称组和用户都应该是复数,例如 groups
和 users
否则您必须在模型上手动指定 table 名称。
此外,为了获取用户的姓名和其他属性,您可以执行类似
的操作
group = Group.find(4)
group_users = group.users
group_users
将为您提供属于 ID 为 4 的组的所有用户的列表。
Suppose if I am currently on group 4, I want to fetch all the users based on that group
@group = Group.find 4
@group.users.each do |user| #-> collection of User objects for @group
user.name
end
您的加入 table 名称错误。
对于has_and_belongs_to_many
,它应该是[alphabetical_first_plural]_[alphabetical_second_plural]
,在你的情况下groups_users
:
class UserGameGroup < ActiveRecord::Migration
def change
create_table :groups_users, id: false do |t|
t.belongs_to :user, index: true
t.belongs_to :group, index: true
end
end
end
如果您想使用现有的 table 名称,则必须在模型中明确定义 join_table
选项:
#app/models/user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :groups, join_table: :user_game_groups
end
#app/models/group.rb
class Group < ActiveRecord::Base
has_and_belongs_to_many :users, join_table: :user_game_groups
end
要填充联接 table,您可以使用 <<
& .delete
methods:
@user = User.find x
@group = Group.find y
@user.groups << @group
@user.groups.delete @group
我有两个表用户和组。
user group
----- -------
id id
name group_name
created_by
在我使用的用户模型中,has_and_belongs_to_many :groups, dependent: :destroy
在我使用的组模型中,has_and_belongs_to_many :users, dependent: :destroy
我已经创建了迁移
class UserGameGroup < ActiveRecord::Migration
def change
create_table :user_game_group, id: false do |t|
t.belongs_to :user, index: true
t.belongs_to :group, index: true
end
end
end
所以在我的组控制器的显示方法中,我想获取特定组的用户。 假设如果我目前在第 4 组,我想获取基于该组的所有用户。
我可以做到这一点 Group.where(group_id: 4)
但它只会给我用户的 ID。有没有办法也获取用户名?
在您当前的示例中,您查询的是一个包含方法 users
的组。因此,同样地,您可以使用此调用来检索用户记录的集合。
group = Group.where(group_id: 4)
group.users # Returns a collection of users.
如果你想进行单个查询,你可以使用 ActiveRecord::QueryMethods
include
这样的方法。
Group.includes(:user).where(group_id: 4)
您应该将加入的群组重命名为 groups_users
。 Rails 希望加入的组采用这种格式(加入的 table 的小字母在前,table 的名称由 _
分隔)。 plus both 应该是复数。此外,您的 table 名称组和用户都应该是复数,例如 groups
和 users
否则您必须在模型上手动指定 table 名称。
此外,为了获取用户的姓名和其他属性,您可以执行类似
的操作 group = Group.find(4)
group_users = group.users
group_users
将为您提供属于 ID 为 4 的组的所有用户的列表。
Suppose if I am currently on group 4, I want to fetch all the users based on that group
@group = Group.find 4
@group.users.each do |user| #-> collection of User objects for @group
user.name
end
您的加入 table 名称错误。
对于has_and_belongs_to_many
,它应该是[alphabetical_first_plural]_[alphabetical_second_plural]
,在你的情况下groups_users
:
class UserGameGroup < ActiveRecord::Migration
def change
create_table :groups_users, id: false do |t|
t.belongs_to :user, index: true
t.belongs_to :group, index: true
end
end
end
如果您想使用现有的 table 名称,则必须在模型中明确定义 join_table
选项:
#app/models/user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :groups, join_table: :user_game_groups
end
#app/models/group.rb
class Group < ActiveRecord::Base
has_and_belongs_to_many :users, join_table: :user_game_groups
end
要填充联接 table,您可以使用 <<
& .delete
methods:
@user = User.find x
@group = Group.find y
@user.groups << @group
@user.groups.delete @group