创建关系时如何动态设置连接模型的属性?
我有一个 has_many :through 通过所有权连接模型之间的用户和项目之间的关系。我希望能够在创建用户和新项目之间的关系时设置所有权模型的属性。到目前为止,这是我所拥有的:
def create
@project = Project.new(params[:project])
if @project.save
current_user.projects << @project
flash[:success] = "Project created!"
redirect_to @project
else
flash[:error] = "Project not created."
end
end
基本上,我不知道在为给定用户创建新项目时如何在所有权模型中设置值“owner_type”,因为我没有直接提及项目中的所有权加入模型创建控制器。我该怎么做?
这是我的所有权(加入)模型:
class Ownership < ActiveRecord::Base
attr_accessible :owner_type
belongs_to :project
belongs_to :user
validates :user_id, :presence => true
validates :project_id, :presence => true
validates :owner_type, :presence => true
end
和我的用户模型:
class User < ActiveRecord::Base
attr_accessible :name, :email, :admin, :projects
has_many :ownerships
has_many :projects, :through => :ownerships
accepts_nested_attributes_for :projects
和我的项目模型:
class Project < ActiveRecord::Base
attr_accessible :name, :description
has_many :ownerships
has_many :users, :through => :ownerships
I have a has_many :through relationship between users and projects via an ownership join model. I want to be able to set an attribute of the ownership model while creating a relationship between a user and a new project. Here is what I have so far:
def create
@project = Project.new(params[:project])
if @project.save
current_user.projects << @project
flash[:success] = "Project created!"
redirect_to @project
else
flash[:error] = "Project not created."
end
end
Basically, I don't know how to set the value "owner_type" in the ownership model when creating a new project for a given user since I don't directly mention the ownership join model in the project creation controller. How do I do that?
Here is my ownership (join) model:
class Ownership < ActiveRecord::Base
attr_accessible :owner_type
belongs_to :project
belongs_to :user
validates :user_id, :presence => true
validates :project_id, :presence => true
validates :owner_type, :presence => true
end
and my User model:
class User < ActiveRecord::Base
attr_accessible :name, :email, :admin, :projects
has_many :ownerships
has_many :projects, :through => :ownerships
accepts_nested_attributes_for :projects
and my Project model:
class Project < ActiveRecord::Base
attr_accessible :name, :description
has_many :ownerships
has_many :users, :through => :ownerships
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键是您在点击
@project.save
之前构建(而不是创建)关联,当您点击“保存”时,项目将首先被持久化,如果成功持久化,所有权也将被创建。The key is that you build (not create) the association before you hit
@project.save
when you hit save the project is persisted first and if it was persisted successfully, the ownership will be created too.编辑:这实际上对我不起作用。
在我的用户模型中,我允许使用此行嵌套属性:
然后,在我的项目#create控制器操作中,我在创建用户和新项目之间的关联时嵌套了一个属性,如下所示:
说实话,我不确定为什么这有效但确实如此。如果有人能解释一下那就太好了。
EDIT: This didn't actually work for me.
In my user model I allow for nested attributes with this line:
Then, in my projects#create controller action, I nested an attribute while creating the association between the user and the new project as so:
To be honest I'm not sure exactly why this works but it does. Would be awesome for someone else to explain it.