【发布时间】:2015-03-23 05:53:50
【问题描述】:
我的应用程序中有用户创建的组。我对如何将创建组的用户设置为所有者感到困惑。我希望能够有多个所有者,所以这是一种“多方通行”的关系。我可以创建/编辑/删除一个组。
所以我的问题是如何在创建组时将当前的 user_id 和 group_id 插入到 group_owners 表中?
这是我目前所拥有的:
用户模型
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :group_owners
has_many :user_groups, through: :group_owners
end
组模型
class UserGroup < ActiveRecord::Base
has_many :goup_owners
has_many :users, through: :groups_owners
validates :name, presence: true, length: {minimum: 5}
validates :visibility, presence: true, length: {minimum: 5}
VISIBILITY_TYPES = ["Public", "Private"]
end
组所有者模型
class GroupOwner < ActiveRecord::Base
belongs_to :user
belongs_to :user_group
end
用户组控制器 - 创建操作
def create
@usergroup = UserGroup.new(usergroup_params)
if @usergroup.save
redirect_to user_groups_path
else
render 'new'
end
end
我认为用户组创建方法中需要进行一些操作,但我不确定是什么。
感谢您提供的任何帮助。
【问题讨论】:
-
你有什么问题?
-
如何在创建用户组时,将当前用户 id 和组 id 添加到组所有者表中?
标签: ruby-on-rails has-many-through