Rails种子数据嵌套属性

时间:2011-02-26 18:07:56

标签: ruby-on-rails ruby-on-rails-3

我在这里做错了什么?表单有效,但在尝试播种数据时,仍然会获得“street1:Symbol”的“未定义方法`to_i”。

EDIT =如果我将所有内容都作为单数地址(has_one而不是has_many),那么种子就可以了。

编辑2 =见其他人的答案......

address.rb
class Address < ActiveRecord::Base
    attr_accessible :street1, :street2, :city, :state, :zipcode, :deleted_at, :addressable_type, :addressable_id, :current, :full_address, :address_type
    belongs_to :addressable, :polymorphic => true
    scope :vendor, where("address_type='Vendor'")

    before_save :update_full_address

    def update_full_address
      unless self.street2.blank?
        street = self.street1 + "<br />" + self.street2 + "<br />" 
      else 
        street = self.street1 + "<br />"
      end
      citystatezip = self.city + ", " + self.state + " " + self.zipcode
      self.full_address = street + citystatezip
    end
end

vendor.rb

class Vendor < ActiveRecord::Base
  attr_accessible :name, :contact, :phone, :addresses_attributes
  has_many :addresses, :as => :addressable
  accepts_nested_attributes_for :addresses, :allow_destroy => true, :reject_if => proc { |obj| obj.blank? }
end

种子数据

require 'faker'

Vendor.delete_all
["Company A", "Company B", "Company C", "Company D"].each do |c|
  params = {:vendor => 
      {
        :name => c,
        :contact => Faker::Name.name,
        :phone => Faker::PhoneNumber.phone_number,
        :addresses_attributes => {
          :street1 => Faker::Address.street_address,
          :city => Faker::Address.city,
          :state => Faker::Address.us_state_abbr,
          :zipcode => Faker::Address.zip_code,
          :address_type => "Vendor"
          }
      }
    } 
  Vendor.create!(params[:vendor]) 
end

在处理has_many时注意数组的[]。

require 'faker'

Vendor.delete_all
["Company A", "Company B", "Company C", "Company D"].each do |c|
  params = {:vendor => 
      {
        :name => c,
        :contact => Faker::Name.name,
        :phone => Faker::PhoneNumber.phone_number,
        :addresses_attributes => [{
          :street1 => Faker::Address.street_address,
          :city => Faker::Address.city,
          :state => Faker::Address.us_state_abbr,
          :zipcode => Faker::Address.zip_code,
          :address_type => "Vendor"
          }]
      }
    } 
  Vendor.create!(params[:vendor]) 
end

1 个答案:

答案 0 :(得分:3)

accepts_nested_attributes_for :foo是您可以创建创建关联记录的表单。当你在代码中构建东西时,没有必要使用它。您可以使用关联名称而不是“address_attributes”创建关联记录。这是一种方法,但Rails确实暴露了一堆做同样事情的方法......

["Company A", "Company B", "Company C", "Company D"].each do |c|
  vendor_address = Address.new :street1 => Faker::Address.street_address,
                               :city => Faker::Address.city,
                               :state => Faker::Address.us_state_abbr,
                               :zipcode => Faker::Address.zip_code,
                               :address_type => "Vendor"

  Vendor.create! :name => c,
                 :contact => Faker::Name.name,
                 :phone => Faker::PhoneNumber.phone_number,
                 :addresses => [vendor_address]
end

如果您想尝试使用嵌套属性方式,那么您不需要哈希的:vendor => {}部分,您可以直接进入参数,并且需要addresses_attributes来是一个数组,而不是哈希。