创建关系时如何动态设置连接模型的属性?

发布于 2024-12-10 01:34:23 字数 1221 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最初的梦 2024-12-17 01:34:23

关键是您在点击 @project.save 之前构建(而不是创建)关联,当您点击“保存”时,项目将首先被持久化,如果成功持久化,所有权也将被创建。

def create
  @project = Project.new(params[:project])
  @project.ownerships.build(:user => current_user, :owner_type => 'chief')

  if @project.save
    flash[:success] = "Project created!"
    redirect_to @project
  else
    flash[:error] = "Project not created."
  end
end

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.

def create
  @project = Project.new(params[:project])
  @project.ownerships.build(:user => current_user, :owner_type => 'chief')

  if @project.save
    flash[:success] = "Project created!"
    redirect_to @project
  else
    flash[:error] = "Project not created."
  end
end
你曾走过我的故事 2024-12-17 01:34:23

编辑:这实际上对我不起作用。

在我的用户模型中,我允许使用此行嵌套属性:

accepts_nested_attributes_for :projects

然后,在我的项目#create控制器操作中,我在创建用户和新项目之间的关联时嵌套了一个属性,如下所示:

current_user.ownerships.create(:owner_type => 'designer', :project => @project)

说实话,我不确定为什么这有效但确实如此。如果有人能解释一下那就太好了。

EDIT: This didn't actually work for me.

In my user model I allow for nested attributes with this line:

accepts_nested_attributes_for :projects

Then, in my projects#create controller action, I nested an attribute while creating the association between the user and the new project as so:

current_user.ownerships.create(:owner_type => 'designer', :project => @project)

To be honest I'm not sure exactly why this works but it does. Would be awesome for someone else to explain it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文