How to convert mongoid criteria to array? - ruby-on-rails

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| ... }

Related

Ruby - Stripe: Missing required param: type

I am trying to add a bank account using Ruby stripe API. but it gives the stripe error "Missing required param: type".
I am using following ruby code:
account = Stripe::Account.create({
:country => 'US',
:managed => true,
:transfer_schedule => {
:interval => 'weekly',
:weekly_anchor => 'friday'
},
:legal_entity => {
:dob => {
:day => birthday.day,
:month => birthday.month,
:year => birthday.year
},
:first_name => first_name,
:last_name => last_name,
:type => 'individual'
},
:tos_acceptance => {
:date => Time.now.to_i,
:ip => request.remote_ip
}
})
You are not passing the proper parameters to the API.
Please check this document for the proper request and response returned by Stripe.
https://stripe.com/docs/api?lang=ruby#create_account
require "stripe"
Stripe.api_key = "sk_test_bcd1234"
Stripe::Account.create(
:type => 'standard',
:country => 'US',
:email => 'bob#example.com'
)
To point out you are not passing :type param in the outer hash. You need to move it to the first level.
account = Stripe::Account.create(
{
:country => 'US',
:managed => true,
:type => 'individual', # Move this from nested to first level
:transfer_schedule => {
:interval => 'weekly',
:weekly_anchor => 'friday'
},
:legal_entity => {
:dob => {
:day => birthday.day,
:month => birthday.month,
:year => birthday.year
},
:first_name => first_name,
:last_name => last_name
},
:tos_acceptance => {
:date => Time.now.to_i,
:ip => request.remote_ip
}
}
)

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.

Minimizing number of Queries

I currently have this:
data = []
products.each do |product|
categories = product.shop_categories.select("shop_categories.id, shop_categories.name").map do |category|
{
:name => category.name,
:category_id => category.id.to_s
}
end
data << {
:name => product.name,
:product_id => product.productid,
:manufacturer => product.manufacturer,
:detail => product.description,
:categories => categories,
:sales_rank => product.sales_rank,
:sale_price => product.sale_price.to_f,
:price => product.price.to_f,
:images => product.images,
:url => product.url,
:is_rated => current_user.voted_for?(product),
:is_liked => current_user.voted_as_when_voted(product),
:is_in_wishlist => current_user.has_product_in_wishlist?(product)
}
end
This part where products are searched for its shop_categories are taking up a huge amount of time to query since every product (100 per run) when it searches for the products' shop_categories.
Is there a way to minimize the number of queries or at least minimize the CPU being used up by this process?
Use includes to eager-load the association:
data = Product.includes(:shop_categories).collect do |product|
{
:name => product.name,
:product_id => product.productid,
:manufacturer => product.manufacturer,
:detail => product.description,
:categories => product.categories.collect { |c| { :name => c.name, :category_id => c.id.to_s } },
:sales_rank => product.sales_rank,
:sale_price => product.sale_price.to_f,
:price => product.price.to_f,
:images => product.images,
:url => product.url,
:is_rated => current_user.voted_for?(product),
:is_liked => current_user.voted_as_when_voted(product),
:is_in_wishlist => current_user.has_product_in_wishlist?(product)
}
end

NULL in (NULL) does not match properly

Using Rails 3 active relation, I have a scope:
scope :duplicate_contact, lambda {|contact| where(
:person_id => contact.person_id,
:salutation => contact.salutation,
:first_name => contact.first_name,
:last_name => contact.last_name,
:suffix => contact.suffix,
:birthday => contact.birthday,
:address => contact.address,
:city => contact.city,
:state => contact.state,
:zip => contact.zip,
:phone_1 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_1_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:phone_2 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_2_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:phone_3 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_3_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:email => [contact.email,contact.alternate_email],
:alternate_email => [contact.email,contact.alternate_email]
)
}
This has a problem when :email is NULL. It returns back zero rows, when in fact it should return at least 1 row, ie duplicate_contact(contact).size == 0 is true when it should be false.
I think this has to do with this staement from the mysql docs: "In SQL, the NULL value is never true in comparison to any other value, even NULL."
How can I get this to return the correct result?
One possible solution that I found:
scope :duplicate_contact, lambda {|contact|
q = where(
:person_id => contact.person_id,
:salutation => contact.salutation,
:first_name => contact.first_name,
:last_name => contact.last_name,
:suffix => contact.suffix,
:birthday => contact.birthday,
:address => contact.address,
:city => contact.city,
:state => contact.state,
:zip => contact.zip
)
[contact.phone_1,contact.phone_2,contact.phone_3].compact.each{|p| q=q.has_phone(p)}
[contact.phone_1_type,contact.phone_2_type,contact.phone_3_type].compact.each{|p| q=q.has_phone_type(p)}
[contact.email,contact.alternate_email].compact.each{|p| q=q.has_email(p)}
q
}
scope :has_phone, lambda {|phone|
where("'#{phone}' IN (phone_1,phone_2,phone_3)")
}
scope :has_phone_type, lambda {|phone|
where("'#{phone}' IN (phone_1_type,phone_2_type,phone_3_type)")
}
scope :has_email, lambda {|email|
where("'#{email}' IN (email,alternate_email)")
}

create_or_update method in rails

if ClassName.exists?(["id = ?", self.id])
object = ClassName.find_by_name(self.name)
object.update_attributes!( :street_address => self.street_address,
:city_name => self.city_name,
:name => self.org_unit_name,
:state_prov_id => self.state_prov_id,
:zip_code => self.zip_code)
else
ClassName.create! :street_address => self.street_address,
:city_name => self.city_name,
:federalid => self.federalid,
:name => self.org_unit_name,
:state_prov_id => self.state_prov_id,
:zip_code => self.zip_code
end
I have code like this. I would like to improve it so that it uses a method, something like create_or_update.
ClassName.create_or_update_by_name(:name => self.name,
:street_address => self.street_address,
:city_name => self.city_name,
:federalid => self.federalid,
:name => self.org_unit_name,
:state_prov_id => self.state_prov_id,
:zip_code => self.zip_code)
If the name exists in the database then it should update that object otherwise it should create a new object.
Is there is any method that exists that I can do this with?
my_class = ClassName.find_or_initialize_by_name(name)
my_class.update_attributes(
:street_address => self.street_address,
:city_name => self.city_name,
:federalid => self.federalid,
:state_prov_id => self.state_prov_id,
:zip_code => self.zip_code
)
As of Rails 6, update_attributes! and update_attributes is deprecated for update! and update, respectively:
my_class.update(
:street_address => self.street_address,
:city_name => self.city_name,
:federalid => self.federalid,
:state_prov_id => self.state_prov_id,
:zip_code => self.zip_code
)
The checked answer above works well for Rails 3. That said the find_or_initialize_by_attribute methods were deprecated in Rails 4. This is the new way. See Rails4 Deprecation warning for find_or_initialize_by method
person = Person.find_or_initialize(name: 'name')
person.update_attributes(other_attrs)
person = Person.find_by_name(name) || Person.new(:name => name)
person.update_attributes!(:street_address => street_address, :city_name => city_name) #etc etc

Resources