59

我想建立一个多态关系accepts_nested_attributes_for。这是代码:

class Contact <ActiveRecord::Base
  has_many :jobs, :as=>:client
end

class Job <ActiveRecord::Base
  belongs_to :client, :polymorphic=>:true
  accepts_nested_attributes_for :client
end

当我尝试访问时Job.create(..., :client_attributes=>{...}给了我NameError: uninitialized constant Job::Client

4

4 回答 4

63

我也遇到了“ArgumentError:无法建立关联模型名称。您是否尝试建立多态一对一关联?”的问题。

我为这类问题找到了更好的解决方案。您可以使用本机方法。让我们看一下 Rails3 内部的 nested_attributes 实现:

elsif !reject_new_record?(association_name, attributes)
  method = "build_#{association_name}"
  if respond_to?(method)
    send(method, attributes.except(*UNASSIGNABLE_KEYS))
  else
    raise ArgumentError, "Cannot build association #{association_name}. Are you trying to build a polymorphic one-to-one association?"
  end
end

那么实际上我们需要在这里做什么呢?只是在我们的模型中创建 build_#{association_name}。我在底部做了完全可行的例子:

class Job <ActiveRecord::Base
  CLIENT_TYPES = %w(Contact)

  attr_accessible :client_type, :client_attributes

  belongs_to :client, :polymorphic => :true

  accepts_nested_attributes_for :client

  protected

  def build_client(params, assignment_options)
    raise "Unknown client_type: #{client_type}" unless CLIENT_TYPES.include?(client_type)
    self.client = client_type.constantize.new(params)
  end
end
于 2011-08-08T22:29:31.333 回答
22

终于让它与 Rails 4.x 一起工作。这是基于 Dmitry/ScotterC 的回答,所以对他们 +1。

步骤 1.首先,这是具有多态关联的完整模型:

# app/models/polymorph.rb
class Polymorph < ActiveRecord::Base
  belongs_to :associable, polymorphic: true

  accepts_nested_attributes_for :associable

  def build_associable(params)
    self.associable = associable_type.constantize.new(params)
  end
end

# For the sake of example:
# app/models/chicken.rb
class Chicken < ActiveRecord::Base
  has_many: :polymorphs, as: :associable
end

是的,这并不是什么新鲜事。但是您可能想知道,doespolymorph_type来自哪里以及它的值是如何设置的?它是基础数据库记录的一部分,因为多态关联将列添加<association_name>_id<association_name>_type表中。就目前而言,build_associable执行时,_type的值为nil.

STEP 2. 传入并接受子类型

让您的表单视图child_type与典型的表单数据一起发送,并且您的控制器必须在其强大的参数检查中允许它。

# app/views/polymorph/_form.html.erb
<%= form_for(@polymorph) do |form| %>
  # Pass in the child_type - This one has been turned into a chicken!
  <%= form.hidden_field(:polymorph_type, value: 'Chicken' %>
  ...
  # Form values for Chicken
  <%= form.fields_for(:chicken) do |chicken_form| %>
    <%= chicken_form.text_field(:hunger_level) %>
    <%= chicken_form.text_field(:poop_level) %>
    ...etc...
  <% end %>
<% end %>

# app/controllers/polymorph_controllers.erb
...
private
  def polymorph_params
    params.require(:polymorph).permit(:id, :polymorph_id, :polymorph_type)
  end

当然,您的视图将需要处理“可关联”的不同类型的模型,但这展示了一个。

希望这可以帮助那里的人。(为什么你还需要多态鸡?)

于 2015-10-02T20:29:47.463 回答
8

上面的答案很好,但不适用于显示的设置。它启发了我,我能够创建一个可行的解决方案:

用于创建和更新

class Job <ActiveRecord::Base
  belongs_to :client, :polymorphic=>:true
  attr_accessible :client_attributes
  accepts_nested_attributes_for :client

  def attributes=(attributes = {})
    self.client_type = attributes[:client_type]
    super
  end

  def client_attributes=(attributes)
    some_client = self.client_type.constantize.find_or_initilize_by_id(self.client_id)
    some_client.attributes = attributes
    self.client = some_client
  end
end
于 2011-08-04T10:04:54.050 回答
5

刚刚发现 rails 不支持这种行为,所以我想出了以下解决方法:

class Job <ActiveRecord::Base
  belongs_to :client, :polymorphic=>:true, :autosave=>true
  accepts_nested_attributes_for :client

  def attributes=(attributes = {})
    self.client_type = attributes[:client_type]
    super
  end

  def client_attributes=(attributes)
    self.client = type.constantize.find_or_initialize_by_id(attributes.delete(:client_id)) if client_type.valid?
  end
end

这使我可以像这样设置我的表单:

<%= f.select :client_type %>
<%= f.fields_for :client do |client|%>
  <%= client.text_field :name %>
<% end %>

不是确切的解决方案,但想法很重要。

于 2010-10-20T07:51:18.113 回答