Nested Rails3 Seed Data Attributes

What am I doing wrong here? Forms work, but when using the "undefined" to_i "method for: street1: Symbol" when trying to sow data.

EDIT = If I do everything as a special address (has_one instead of has_many), the seed works.

EDIT 2 = see answer below for others ...

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 

seed data

 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 

Pay attention to [] for the array when working 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 
+4
source share
1 answer

accepts_nested_attributes_for :foo is that you can create forms that create related records. When you build things in code, there is no need to use this. You can create related records using association names instead of address_attributes. Here's one way to do this, but Rails really reveals a bunch of ways to do the same ...

 ["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 

If you want to try and use the nested attribute method, you don’t need the hash part :vendor => {} , you can go directly to the parameters, and you need addresses_attributes be an array, not a hash.

+3
source

Source: https://habr.com/ru/post/1341453/


All Articles