Activerecord 使用类名的几个多态关联?

Activerecord 使用类名的几个多态关联?,activerecord,ruby-on-rails-3.2,nested-forms,nested-attributes,polymorphic-associations,Activerecord,Ruby On Rails 3.2,Nested Forms,Nested Attributes,Polymorphic Associations,我正在尝试创建一篇文章,使用rails多态关联和嵌套形式保存in_prices和out_prices(每个国家一个) 我有一个如下所示的数据模型: # app/models/article.rb class Article < ActiveRecord::Base has_many :out_prices, :class_name => "Price", :as => :priceable has_many :in_prices, :class_name =&g

我正在尝试创建一篇文章,使用rails多态关联和嵌套形式保存in_prices和out_prices(每个国家一个)

我有一个如下所示的数据模型:

# app/models/article.rb
class Article < ActiveRecord::Base
    has_many :out_prices, :class_name => "Price", :as => :priceable
    has_many :in_prices, :class_name => "Price", :as => :priceable
end

# app/models/price.rb
class Price < ActiveRecord::Base
    belongs_to :priceable, :polymorphic => true
end

# db.schema for prices
create_table "prices", :force => true do |t|
    t.integer  "value"
    t.integer  "country_id"
    t.integer  "priceable_id"
    t.string   "priceable_type"
end
# db.schema for prices
create_table "prices", :force => true do |t|
    t.integer  "value"
    t.integer  "price_type"
    t.integer  "country_id"
    t.integer  "priceable_id"
    t.string   "priceable_type"
end
创建新文章时,需要设置文章以确保显示空的价格表单字段(每个国家一个)

现在谈谈实际问题。当我编辑一篇已经创建的文章(其中包含相关联的in_prices和out_prices)时,Rails将无法区分这些不同类型的多态关联(in_prices和out_prices)。因此,这两个嵌套表单帮助器都会呈现所有关联价格的表单字段,这不是所需的行为。我只想在其中一个嵌套表单中列出输入价格,在另一个表单中列出输出价格

如何配置这些关联以确保rails能够区分两个不同嵌套表单帮助程序中的in_prices和out_prices关联

编辑已解决

我的一位朋友指出,我需要在价格表中添加另一个字段来标记它是什么类型的价格。我将此字段称为price_类型,db.schema的结果如下所示:

# app/models/article.rb
class Article < ActiveRecord::Base
    has_many :out_prices, :class_name => "Price", :as => :priceable
    has_many :in_prices, :class_name => "Price", :as => :priceable
end

# app/models/price.rb
class Price < ActiveRecord::Base
    belongs_to :priceable, :polymorphic => true
end

# db.schema for prices
create_table "prices", :force => true do |t|
    t.integer  "value"
    t.integer  "country_id"
    t.integer  "priceable_id"
    t.string   "priceable_type"
end
# db.schema for prices
create_table "prices", :force => true do |t|
    t.integer  "value"
    t.integer  "price_type"
    t.integer  "country_id"
    t.integer  "priceable_id"
    t.string   "priceable_type"
end
注意:不要将此字段命名为“type”,因为这是一个保留名称

“price_type”字段可以通过在嵌套表单中添加隐藏字段(不太安全)来填充,也可以在保存项目及其关联数据之前在控制器中进行处理。我选择将其添加为隐藏参数,如:

# app/views/articles/_article_form.html.erb
<%= form_for setup_article(@article) do |f| %>
    <%= f.fields_for :in_prices do |ff| %>
        <%= ff.text_field :value %>
        <%= ff.text_field :price_type, :value => "in" %>
    <% end %>
    <%= f.fields_for :out_prices do |ff| %>
        <%= ff.text_field :value %>
        <%= ff.text_field :price_type, :value => "out" %>
    <% end %>
<% end %>
#app/views/articles/_article_form.html.erb
“在”%>
“输出”%>
要确保正确过滤关联,需要使用“:conditions”标记声明关联,如:

# app/models/article.rb
class Article < ActiveRecord::Base
    has_many :out_prices, :class_name => "Price", :as => :priceable, :conditions => { :price_type => "in" } 
    has_many :in_prices, :class_name => "Price", :as => :priceable, :conditions => { :price_type => "out" }
end
#app/models/article.rb
类文章“Price”,:as=>:priceable,:conditions=>{:Price\u type=>“in”}
有很多:in\u prices,:class\u name=>“Price”,:as=>:priceable,:conditions=>{:Price\u type=>“out”}
终止
。。现在一切都按预期进行。干杯