Rails3 种子数据嵌套属性 [英] Rails3 seed data nested attribute

查看:29
本文介绍了Rails3 种子数据嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做错了什么?表单可以工作,但在尝试播种数据时会不断收到未定义的方法 `to_i' for :street1:Symbol".

What am I doing wrong here? The forms work but keep getting "undefined method `to_i' for :street1:Symbol" when trying to seed data.

EDIT = 如果我以单一地址(has_one 而不是 has_many)的方式做所有事情.

EDIT 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

供应商.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 时注意数组的 [].


Note the [] for an array when dealing with 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

推荐答案

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

accepts_nested_attributes_for :foo is so that you can create forms which create associated records. When you're building things in code, there's no need to use this. You can create the associated records using the association names instead of "address_attributes". Here's one way of doing it, but Rails does expose a bunch of ways of doing this same thing...

["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 是一个数组,而不是一个散列.

If you are wanting to try and use the nested attributes way, then you don't need the :vendor => {} part of the hash, you can go straight into the params, and you need addresses_attributes to be an array, not a hash.

这篇关于Rails3 种子数据嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆