Rails3 seed data nested attribute - ruby-on-rails

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 = If I do everything as a singular address (has_one instead of has_many) 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
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 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
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.

Related

How to convert mongoid criteria to array?

I have a mongoid criteria categories and I need to convert to an array. I'm using categories.to_a but this dont works and always that the mongoid criteria is iterate by .map it's doing a .find a new query.
How can I fix this?
def self.mapOffers (array, user)
array.map { |u|
{
:id => u.id.to_s,
:name => u.name,
:description => u.description,
:price => u.price,
:url => u.url,
:categories => Category.mapCategories(u.categories.to_a, user),
:picture => u.picture.url,
:accepts_cash => u.accepts_cash_transactions,
:location => {
:longitude => u.longitude,
:latitude => u.latitude,
:street => u.street,
:neighborhood => u.neighborhood,
:number => u.number,
:zip => u.zip,
:city => u.city,
:state => u.state,
:complement => u.complement,
:country => u.country,
},
:fixedMeetingPoint => u.fixedMeetingPoint,
:meetingPoint => {
:street => u.meetingPointStreet,
:neighborhood => u.meetingPointNeighborhood,
:number => u.meetingPointNumber,
:zip => u.meetingPointZip,
:city => u.meetingPointCity,
:state => u.meetingPointState,
:complement => u.meetingPointComplement,
:country => u.meetingPointCountry,
:latitude => u.meetingPointLatitude,
:longitude => u.meetingPointLongitude,
},
:notes => u.notes,
}}
end
def self.mapCategories (array, user)
array.map { |u| {
:id => u.id.to_s,
:name => u.name,
:selected => !user.nil? && u.users.include?(user),
:picture => u.picture.url,
}}
end
Starting from criteria:
scope = Band.where(name: 'foo')
... retrieve the complete result set from the database and store in an array:
bands = scope.to_a
... then iterate the array any number of times:
bands.each { |band| ... }
bands.each { |band| ... }

Rails Nested forms, not add a second element if user dont clic on "add new"

Hi im using Rails Nested forms, i need something very simple, but i cant figure put why, cause im a noob in Rails.
In the nested forms, when the user select an option on the list, the script automatically insert a second select list, in blank, this blanks remains always there,
Is there a way to avoid this.. so if you select nothing happends, (just the selection ofcourse) until you hit "add new", just then the script add a new select list for user to add another option.
Thanks..
View code
<%= f.fields_for :citizens do |citizen_form| %>
<div>
<%= citizen_form.label :citizen, t('generales.citizen') %>
<%= citizen_form.select :country_id , Country.all.collect {|p| [ t("generales."+p.iso), p.id ] }, { :include_blank => true } , { :class => 'pca33' } %>
<div id="delerr"><%= citizen_form.link_to_remove t('generales.delete') %></div>
</div>
<% end %>
<%= f.link_to_add t('generales.add'), :citizens %>
Model Player.rb
class Player < ActiveRecord::Base
belongs_to :user
has_many :clubs
has_many :links
has_many :references
has_many :achievements
has_many :citizens
has_and_belongs_to_many :languages
has_and_belongs_to_many :selections
accepts_nested_attributes_for :clubs, :allow_destroy => true, :reject_if => proc { |attributes| attributes['name'].blank? }
accepts_nested_attributes_for :links, :allow_destroy => true, :reject_if => proc { |attributes| attributes['url'].blank? }
accepts_nested_attributes_for :references, :allow_destroy => true, :reject_if => proc { |attributes| attributes['name'].blank? }
accepts_nested_attributes_for :achievements, :allow_destroy => true, :reject_if => proc { |attributes| attributes['name'].blank? }
accepts_nested_attributes_for :citizens, :allow_destroy => true, :reject_if => proc { |attributes| attributes['country_id'].blank?}
attr_accessible :name,
:lastname,
:birthday,
:height,
:height_measure,
:weight,
:weight_measure,
:inches,
:city,
:birthplace,
:other_languages,
:cp,
:phone,
:cellphone,
:web_page,
:game_status,
:club,
:actual_club,
:actual_country_club,
:actual_division_club,
:actual_contract_expiration_club,
:last_club,
:last_country_club,
:last_division_club,
:last_contract_expiration_club,
:position,
:alternative_position,
:dominant_leg,
#normal player
:short_passes,
:long_passes,
:shots_half_distance,
:shots_long_distance,
:ball_habilities,
:offensive_capability,
:ball_driving,
:defense_capability,
:dribbling,
:velocity,
:vision_field,
:movements_wothout_ball,
:recovery_ball,
:head_ball,
:lidership,
:teamwork,
#goalkeeper
:air_game,
:clearance_technique, #técnica de despeje
:ball_keep, #atajes
:flexibility, #flexibilidad
:penalty_keep, #atajar penales
:achique,
:defense_communication,
:foot_game,
:velocity_reaction, #reflejos
:area_domination,
:goalkeep_teamwork,
:goalkeep_lidership,
:strenghts,
:weaknesses,
:aditional_information,
:active,
:clubs_attributes,
:links_attributes,
:references_attributes,
:achievements_attributes,
:citizens_attributes,
:avatar_file_name,
:avatar_content_type,
:avatar_file_size,
:avatar,
:language_ids,
:selection_ids
POSITIONS = %w{
goalkeeper
defense
medium
offensive
}
LEG = %w{
left
right
both
}
# altura
HEIGHT = (1..200).to_a
INCH = (1..11).to_a
# peso
WEIGHT = (1..300).to_a
HEIGHT_MEASURE = %w{
cms
pies
}
WEIGHT_MEASURE = %w{
kgs
lbs
}
Paperclip.interpolates :random_hex do |attachment, style|
attachment.instance.random_hex
end
has_attached_file :avatar, :styles => { :profile => "300x300", :thumb => "100x100#"},
:url => "/assets/people/:id/:style/:hash.:extension",
:path => ":rails_root/public/assets/people/:id/:style/:hash.:extension",
:hash_secret => "longSecretString"
validates_attachment_size :avatar, :less_than => 2.megabytes # Solo aceptar imágenes menores a 2 Mb.
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif']
def defeated?
t = Time.now - created_at
mm, ss = t.divmod(60)
hh, mm = mm.divmod(60)
dd, hh = hh.divmod(24)
dd > 180 ? true : false
end
end
Model citizen.rb
class Citizen < ActiveRecord::Base
attr_accessible :country_id
belongs_to :player
end

Rails rabl condition for class variable in view

In controller i have such code:
#bank_exchangers = ExchangerList.find(:all, :conditions => {:bank_id => params[:id]})
#currency_list = CurrencyList.all
#currencies = []
#currency_list.each do |c|
#currencies << CurrencyValue.find(:all, :conditions => {:currency_list_id => c.id}, :order => :updated_at)
end
#currencies.flatten!
and i have such models:
class CurrencyList < ActiveRecord::Base
attr_accessible :code, :country, :name
has_many :currency_values
end
class CurrencyValue < ActiveRecord::Base
attr_accessible :currency_list_id, :direction_of_exchange_id, :value
belongs_to :currency_list
belongs_to :exchanger_list
end
class ExchangerList < ActiveRecord::Base
attr_accessible :address, :bank_id, :exchanger_type_id, :latitude, :location_id, :longitude, :name
has_many :currency_values
end
i need to display for each ExchangerList it's CurrencyValue with some conditions, as i provided below... But main trouble is with rabl output:
i have such code:
collection #bank_exchangers, :root => "bank_exchangers", :object_root => false
attributes :id, :name, :address, :location_id, :latitude, :longitude, :exchanger_type_id
child #currencies do
attribute :value
end
as you can see, here for each #bank_exchangers i create node with it's #currencies... But i need to display node only for this #bank_exchangers iterator, if i would write in controller i would write something like:
#currencies << CurrencyValue.find(:all, :conditions => {:currency_list_id => c.id, :exchanger_list_id => param}, :order => :updated_at)
How to set something like this in view?
Becouse now my output is like:
{"bank_exchangers":[{"id":3,"name":"Банк *** ЦБУ №1","address":"г. Минск, ул. Московская, 13","location_id":8,"latitude":null,"longitude":null,"exchanger_type_id":1,"location_name":"Минск","exchanger_type_name":"normal","currency_values":[{"currency_value":{"value":8620.0}},{"currency_value":{"value":8620.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":8620.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":11500.0}},{"currency_value":{"value":11100.0}}]},{"id":4,"name":"Банк ***","address":"г. Минск, Мясникова, 32","location_id":8,"latitude":null,"longitude":null,"exchanger_type_id":1,"location_name":"Минск","exchanger_type_name":"normal","currency_values":[{"currency_value":{"value":8620.0}},{"currency_value":{"value":8620.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":8620.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":11500.0}},{"currency_value":{"value":11100.0}}]}]}
as you can see, for each bank_exchangers i create node with all currency_values data, but i need to put for each bank_exchangers in node only currency_values for this bank_exchangers parent....
How could i do this?
Sorry if something is not clear... i'm new...
Just how to set for my child #currencies in view some condition?
You can take advantage of the relation between ExchangerList and CurrencyValues:
child :currency_values do
attribute :value
end
If you have conditions, you can include these using a lambda:
if => lambda { |child| child.something? })
child(:currency_values, if => lambda { |currency_value| currency_value.something? }) do
attribute :value
end
You may also want to define a view for just the one ExchangerList object (i.e. show):
object #bank_exchanger, :object_root => false
attributes :id, :name, :address, :location_id, :latitude, :longitude, :exchanger_type_id
child(:currency_values, if => lambda { |currency_value| currency_value.something? }) do
attribute :value
end
Then simply have the collection extend this:
collection #bank_exchangers, :root => "bank_exchangers", :object_root => false
extends 'bank_exchangers/show'
Alternatively, if you add a 'currencies' method to to the ExchangerList model, you can call the method directly as an attribute via RABL. Some test code I wrote:
def test
test_array = []
1.upto(3) do |i|
test_array << {qty: i, px: 2*i}
end
return test_array
end
You can then simply call this as an attribute:
object #object
attribute :test
This results in the following JSON, which I believe is similar to the format you are trying to achieve:
test: [
{
qty: 1,
px: 2
},
{
qty: 2,
px: 4
},
{
qty: 3,
px: 6
}
],

rails 3 custom validation error messages in a join table, how?

How can I return errors messages from a cross reference table with multiple records when I trying to create those? I'm trying this:
## activity_set.rb
class ActivitySet < ActiveRecord::Base
has_many :activity_set_lessons
has_many :lessons, :through => :activity_set_lessons
validates :name, :presence => true
def activity_set_lessons=(data)
data.each_with_index do |v, i|
activity_set_lessons.build(
:lesson_id => v[:lesson_id],
:sort_order => i,
:weight_percentage => v[:weight_percentage]
)
end
end
end
## activity_set_lesson.rb
class ActivitySetLesson < ActiveRecord::Base
belongs_to :activity_set
belongs_to :lesson
validates :lesson_id, :presence => true
validates_each :weight_percentage do |record, attr, value|
record.errors.add :base, "woot" if value.blank?
end
end
This is the request data:
## params[:activity_set]
"activity_set" => {
"name" => "hshshshs",
"keywords" => "",
"activity_set_lessons" => [
{"weight_percentage" => "", "lesson_id"=>"4"},
{"weight_percentage" => "", "lesson_id"=>"5"}
]
}
Error messages from #activity_set when I do #save:
{
"errors":{
"activity_set_lessons":["is invalid","is invalid"]
},
"full_messages":[
"Activity set lessons is invalid","Activity set lessons is invalid"
]
}
I always got the same error message even if I'm adding a custom one in the join table. How can I return a message like: "woot 1 is wrong" or something like that, per validation?.
Thanks.
make use of accepts_nested_attributes_for
## activity_set.rb
class ActivitySet < ActiveRecord::Base
has_many :activity_set_lessons
has_many :lessons, :through => :activity_set_lessons
validates :name, :presence => true
accepts_nested_attributes_for :activity_set_lessons
end
view will look like
= form_for #activity_set do |f|
[activity_set form fields ]
= f.fields_for :activity_set_lessons do |p|
= p.select :lession_id
= p.select :weight_percentage

How can I assign users to certain seed data?

I have the following data in my seeds.rb file:
users = User.create([
{
:email => 'user1#email.com',
:password => 'test',
:password_confirmation => 'test'
},
{
:email => 'user2#email.com',
:password => 'test',
:password_confirmation => 'test'
}
])
puts 'Users added'
UserPrice.create([
{
# assign user 1
:product_name => "Great Value Vitamin D Whole Milk",
:price => '3.81',
:purchase_date => Date.strptime("08/25/2011", "%m/%d/%Y"),
:store => "New York"},
{
#assign user 2
:product_name => 'Eggs',
:price => '2.78',
:purchase_date => Date.strptime("08/25/2011", "%m/%d/%Y"),
:store => "New York"
}
])
puts 'Added Prices'
How do I assign the rightful users to the UserPrices in my seeds.rb?
Note: I tried to do :user => users.first but that didn't work.
Working Code:
user1 = User.create(:email => 'user1#email.com', :password => 'qweasd', :password_confirmation => 'qweasd')
user2 = User.create(:email => 'user2#email.com',:password => 'qweasd',:password_confirmation => 'qweasd')
user1.user_prices.create(
:product_name => "Great Value Vitamin D Whole Milk",
:price => '3.81',
:purchase_date => Date.strptime("08/25/2011", "%m/%d/%Y"),
:store => "New York"
)
user2.user_prices.create(
:product_name => 'Eggs',
:price => '2.78',
:purchase_date => Date.strptime("08/25/2011", "%m/%d/%Y"),
:store => "New York"
)
You might want to do this more along these lines:
user = User.create(#stuff#)
user.user_prices.create(#stuff#)
Assuming a has_many relation.

Resources