【发布时间】:2013-12-21 14:28:01
【问题描述】:
在我的 rails 3.2 站点中,我有用户和社区。当用户创建新社区时,我希望用户也自动成为该社区的成员。会员制形成了另一种模式。关系如下:
用户:
has_many :memberships
has_many :communities, :through => :memberships
社区:
has_many :memberships
has_many :users, :through => :memberships
会员:
belongs_to :user
belongs_to :community
那么简而言之,我如何在使用 user_id 和 member_id 创建社区的同时创建会员?
更新
控制器动作:
def create
@community = Community.new(params[:community])
respond_to do |format|
if @community.save
format.html { redirect_to @community, notice: 'Community was successfully created.' }
format.json { render json: @community, status: :created, location: @community }
else
format.html { render action: "new" }
format.json { render json: @community.errors, status: :unprocessable_entity }
end
end
结束
【问题讨论】:
标签: ruby-on-rails-3 has-many-through