我的 has_many :through 关联用户、组和成员资格在某些方法中造成了麻烦
我正在使用 has_many :through 关联:用户通过会员资格加入组。我在用户模型中创建的一些方法无法确定用户是否是组的成员,并允许用户加入和离开组。使用控制台,memberships.find_by_group_id
始终返回 nil。我不确定为什么,我认为这可能是我通过关联设置 has_many 的方式,尽管经过多次查看并咨询 Railscasts/博客后似乎还可以。如果您希望我发布更多信息,例如 schema.db 或其他内容,请告诉我
class User < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
.
.
.
def member?(group)
memberships.find_by_group_id(group)
end
def join!(group)
memberships.create!(:group_id => group.id)
end
def leave!(group)
memberships.find_by_group_id(group).destroy
end
.
.
.
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships, :source => :user
has_attached_file :photo, :styles => { :thumb => "100x100",
:small => "200x200" }
attr_accessible :name, :description, :private, :created_at, :group_id
attr_accessible :photo, :photo_file_name, :photo_content_type,
:photo_file_size, :photo_updated_at
end
class Membership < ActiveRecord::Base
attr_accessible :group_id
belongs_to :user
belongs_to :group
end
这是成员资格控制器:
class MembershipsController < ApplicationController
def create
@group = Group.find(params[:membership][:group_id])
current_user.join!(@group)
respond_to do |format|
format.html { redirect_to @group }
format.js
end
end
def destroy
@group = Membership.find(params[:id]).group
current_user.leave!(@group)
respond_to do |format|
format.html { redirect_to @group }
format.js
end
end
def index
@memberships = Membership.all
end
end
I am working with a has_many :through association: Users join Groups through a Membership. I am having trouble with some methods I created in the user model to determine if the user is a member of a group, and to allow a user to join and leave a group. Using the console, memberships.find_by_group_id
always returns nil. I am not sure why and I think it might be the way I have set up my has_many :through associations, though after looking it over many, many times and consulting railscasts/blogs it seems ok. If you want me to post more info, like the schema.db or something, let me know
class User < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
.
.
.
def member?(group)
memberships.find_by_group_id(group)
end
def join!(group)
memberships.create!(:group_id => group.id)
end
def leave!(group)
memberships.find_by_group_id(group).destroy
end
.
.
.
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :members, :through => :memberships, :source => :user
has_attached_file :photo, :styles => { :thumb => "100x100",
:small => "200x200" }
attr_accessible :name, :description, :private, :created_at, :group_id
attr_accessible :photo, :photo_file_name, :photo_content_type,
:photo_file_size, :photo_updated_at
end
class Membership < ActiveRecord::Base
attr_accessible :group_id
belongs_to :user
belongs_to :group
end
Here is the membership controller:
class MembershipsController < ApplicationController
def create
@group = Group.find(params[:membership][:group_id])
current_user.join!(@group)
respond_to do |format|
format.html { redirect_to @group }
format.js
end
end
def destroy
@group = Membership.find(params[:id]).group
current_user.leave!(@group)
respond_to do |format|
format.html { redirect_to @group }
format.js
end
end
def index
@memberships = Membership.all
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在寻找的是
:has_and_belongs_to_many
(NN) 关联而不是:has_many
(1-N) 关联。似乎每个组也可以有很多用户。
查看官方 Rails 指南 http://guides.rubyonrails.org/association_basics .html#has_and_belongs_to_many-association-reference
使用:has_and_belongs_to_many 关系,将一个集合(用户)添加到另一个集合(成员资格)将非常容易。
例如,
会将 current_user 添加到成员资格中
,并将
从成员资格中删除 current_user。
I think what you are looking for is
:has_and_belongs_to_many
(N-N) association rather than:has_many
(1-N) association.It seems that each group can have many users as well.
Take a look at official rails guides at http://guides.rubyonrails.org/association_basics.html#has_and_belongs_to_many-association-reference
Using :has_and_belongs_to_many relationships, it will be really easy to add a collection(user) to the other collection(membership) .
For example
will add current_user to the membership
and
will delete current_user from the membership.