I'm using the active_shipping gem on my rails app and am getting a very strange error reading:
amount must be a Numeric
The error is being called on my shipping method:
def shipping
#user = current_user
#products = current_order.order_items.all
#order = current_order
packages = []
#products.each do |thing|
packages << ActiveShipping::Package.new( thing.product.weight * 16, <<<<<<<<<<ERROR CALLED ON THIS LINE
[thing.product.box_length, thing.product.box_width, thing.product.box_depth],
units: :imperial)
end ## each do
origin = ActiveShipping::Location.new( country: 'US', state: 'CO', city: 'Sedalia', zip: '80135')
if #user.country == 'US'
destination = ActiveShipping::Location.new( country: #user.country, state: #user.state, city: #user.city, zip: #user.zip)
else
destination = ActiveShipping::Location.new( country: #user.country, province: #user.state, city: #user.city, postal_code: #user.zip)
end # if/else for country
ups = ActiveShipping::UPS.new(login: 'lizbayardelle', password: 'UPSpassw0rd', key: '3D287D7B39D0D398')
ups_response = ups.find_rates(origin, destination, packages)
#ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
usps = ActiveShipping::USPS.new(login: '380LINCH6422')
usps_response = usps.find_rates(origin, destination, packages)
#usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
end
The value for the product weight is in the seeds.rb file:
Product.delete_all
Product.create! id: 1, name: "Rule #23 Tee Shirt (Small)", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Size Small. Spread the word. These tee shirts not only have the Manly Art crest, but they also have a man rule on the back to remind any passers-by that may have forgotten.", active: true
Product.create! id: 2, name: "Rule #23 Tee Shirt (Medium)", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Size Medium. Spread the word. These tee shirts not only have the Manly Art crest, but they also have a man rule on the back to remind any passers-by that may have forgotten.", active: true
Product.create! id: 3, name: "Rule #23 Tee Shirt (Large)", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Size Large. Spread the word. These tee shirts not only have the Manly Art crest, but they also have a man rule on the back to remind any passers-by that may have forgotten.", active: true
Product.create! id: 4, name: "Rule #23 Tee Shirt (XL)", weight: 1, box_width: 12, box_length: 14, box_depth: 3, price: 32.00, short_description: "Size XL. Spread the word. These tee shirts not only have the Manly Art crest, but they also have a man rule on the back to remind any passers-by that may have forgotten.", active: true
Product.create! id: 5, name: "Grill Glove", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Protect your manly mitts from the fiery dangers of the grill.", active: true
Product.create! id: 6, name: "BBQ Apron", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Standard grilling apron with three pockets for all your necessary tools. You get to add the grease marks and stains from the marinade of your choosing.", active: true
Can anyone see what's going on here? Google/Stack Overflow don't seem to have heard of this error, which is worrisome...
I ended up adding .to_i to each thing and it made it work for some reason.
Related
Hey iam trying to figure out how i could limit my combinations of objects.
My problem:
I want to generate x teams with the most "point" outcome of 432 players, max team size is 7 players. Each player has an "position" attribute, every team must have each position one time and max 3 times. Also an player has an "worth" attribute. Each team cant be more worth than 70. Every player can only be one time in an team.
Data example:
{ name: 'James Harden', position: 'PG', weight: 15.9, points: 62.63 },
{ name: 'Russell Westbrook', position: 'PG', weight: 14.9, points: 56.12 },
{ name: 'LeBron James', position: 'SF', weight: 15.9, points: 55.67 },
{ name: 'Bradley Beal', position: 'SG', weight: 14.8, points: 52.69 },
{ name: 'Anthony Davis', position: 'PF', weight: 14.6, points: 52.16 },
{ name: 'Damian Lillard', position: 'PG', weight: 13.5, points: 49.34 },
{ name: 'Nikola Vucevic', position: 'C', weight: 12.9, points: 47.97 },
{ name: 'Domantas Sabonis', position: 'PF', weight: 12.8, points: 47.6 }
My try:
Well i tried to use the combination method from ruby:
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
print b.combination(7).to_a
that take 52 minutes for my laptop so solve all combinations. so i search for a way to do it faster an to apply more options like mentioned above to limit the result of teams.
Later if i find an solution i want to build this thing into an rails app with picture of the players and so on. Hopefully someone can give me an hint or has done something similar to give advices.
Also how would i controll to use more cpu kernels to speed up the proccess ?
[#<ActiveRecord::Associations::CollectionProxy [#<InvoiceServiceType id: 1, value_charged: 50.0, invoice_id: 4, service_type_id: 1>, #<InvoiceServiceType id: 2, value_charged: 50.4, invoice_id: 4, service_type_id: 2>]>, #<ActiveRecord::Associations::CollectionProxy [#<InvoiceServiceType id: 8, value_charged: 70.0, invoice_id: 1, service_type_id: 2>, #<InvoiceServiceType id: 9, value_charged: 50.0, invoice_id: 1, service_type_id: 6>]>]
I want to sum all value_charged .
im try map(&:value_charged).sum
collect..
and nothing :(
Thank you
Lets say that you have a #object with a has_many relationship details, and details have a field call :value_charged, you could try sum all the :value_charged of #object.details with:
#object.details.sum(:value_charged)
I have table with some columns: id, user_id, message_id, message_type; for example:
id: 1, user_id: 1, message_id: 4, message_type: 'Warning'
id: 2, user_id: 1, message_id: 5, message_type: 'Warning'
id: 3, user_id: 1, message_id: 6, message_type: 'Warning'
id: 4, user_id: 2, message_id: 4, message_type: 'Error'
id: 5, user_id: 2, message_id: 1, message_type: 'Exception'
id: 6, user_id: 1, message_id: 2, message_type: 'Exception'
id: 7, user_id: 1, message_id: 3, message_type: 'Exception'
id: 8, user_id: 2, message_id: 4, message_type: 'Exception'
I want to get grouping result like news in social networks. On columns user_id and message_type, while message_type repeating. And need LIMIT 20 ORDER BY id DESC.
Example:
id: 8, user_id: 2, message_id: 4, message_type: 'Exception'
id: {6,7} user_id: 1, message_id: {2,3}, message_type: 'Exception'
id: 5, user_id: 2, message_id: 1, message_type: 'Exception'
id: 4, user_id: 2, message_id: 4, message_type: 'Error'
id: {1, 2, 3}, user_id: 1, message_id: {4, 5, 6}, message_type: 'Warning'
How to do it with best performance?
I found only 1 way:
With window function lead() find a moment when was changed dict (user, message type)
With window function sum() set sequnce number for each new dict
Group by sequence and select what you need:
Checking:
create table test (
id serial primary key,
user_id integer,
message_id integer,
message_type varchar
);
insert into test (user_id, message_id, message_type)
values
(1, 4, 'Warning'),
(1, 5, 'Warning'),
(1, 6, 'Warning'),
(2, 4, 'Error'),
(2, 1, 'Exception'),
(1, 2, 'Exception'),
(1, 3, 'Exception'),
(2, 4, 'Exception')
;
select
array_agg(grouped.id) as record_ids,
grouped.user_id,
array_agg(grouped.message_id) as message_ids,
grouped.message_type
from (
select changed.*,
sum(changed.changed) over (order by changed.id desc) as group_n
from (
select tt.*,
case when lag((user_id, message_type)) over (order by tt.id desc) is distinct from (user_id, message_type) then 1 else 0 end as changed
from test tt
) changed
order by id desc
) grouped
group by grouped.group_n, grouped.user_id, grouped.message_type
order by grouped.group_n
;
Result:
record_ids | user_id | message_ids | message_type
------------+---------+-------------+--------------
{8} | 2 | {4} | Exception
{7,6} | 1 | {3,2} | Exception
{5} | 2 | {1} | Exception
{4} | 2 | {4} | Error
{3,2,1} | 1 | {6,5,4} | Warning
(5 rows)
The array_agg function should do the trick:
SELECT user_id,
message_type,
ARRAY_AGG (DISTINCT id),
ARRAY_AGG (DISTINCT message_id)
FROM mytable
GROUP BY user_id, message_type
I have a notifications table where I email out reports to people based on the frequency they've selected.
If an email address exists across multiple questions and has the same frequency...then I need to group them together so I can send one report for both questions.
[
#<Notification id: 1, question_id: 58, email: "john#example.com", frequency: "daily">,
#<Notification id: 2, question_id: 25, email: "john#example.com", frequency: "daily">,
#<Notification id: 3, question_id: 47, email: "john#example.com", frequency: "monthly">,
#<Notification id: 3, question_id: 19, email: "frank#example.org", frequency: "monthly">
]
So in my example data, 3 reports would be sent:
1 to john#example.com for question's 58 and 25 on a daily basis
1 to john#example.com for question 47 on a monthly basis
1 to frank#example.org for question 19 on a monthly basis
I may not be explaining this very well, so let me know if something needs clarification.
You can achieve this with a regular group_by:
#notifications = your_method_to_retrieve_notifications
#notifications = #noticications.group_by do |notif|
notif.ferquency + ':' + notif.email
end
This will group your notifications like this:
#notifications = {
'daily:john#example.com' => [<Notification id: 1>, #etc.],
'monthly:john#example.com' => [# list of notifications],
'monthly:frank#example.org' => [# list of notif]
}
If you want only an array of list of notifications grouped by frequency & email, use the above method and add this:
#notifications = your_method_to_retrieve_notifications
#notifications = #noticications.group_by do |notif|
notif.ferquency + ':' + notif.email
end.values
# returns Array of arrays like:
[
[<Notification id: 1 frequency: "daily", email "a#b.com">,<Notification id: 2 frequency: "daily", email "a#b.com">],
[<Notification id: 3 frequency: "daily", email "xx#yy.com">],
[<Notification id: 4 frequency: "monthly", email "a#b.com">,<Notification id: 5 frequency: "monthly", email "a#b.com">],
]
I am using Mongoid, Rails and Fabrications and at a total loss with how this is happening. Any thoughts very appreciated, but I know this pretty complicated. I just want to fabricate a user and have only four joined groups, but I keep getting eight loaded.
Here is the relevant section of my code
#user1 = Fabricate.build(:registered)
#user1.joined_groups << [common_group,
cali_group,
ca46,
Fabricate(:polco_group, {:name => "Gang of 13", :type => :custom})]
When I run #user1.joined_groups.size I get 4, but when I do #user1.joined_groups.map(&:name), I get 8 records:
#<PolcoGroup _id: 1 ... member_ids: [], follower_ids: []>
#<PolcoGroup _id: 1 ... member_ids: [], follower_ids: []>
#<PolcoGroup _id: 1 ... member_ids: [], follower_ids: []>
#<PolcoGroup _id: 1 ... member_ids: [], follower_ids: []>
#<PolcoGroup _id: 1 ... member_ids: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], follower_ids: [1, 1]>
#<PolcoGroup _id: 1 ... member_ids: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], follower_ids: [1, 1]>
#<PolcoGroup _id: 1 ... member_ids: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], follower_ids: [1, 1]>
#<PolcoGroup _id: 1 ... member_ids: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], follower_ids: [1, 1]>
(where i have replaced all BSON::ObjectId('4eab3ca5f11aac2701000009') statements with ones and removed a lot of the middle code.
The full set of code is available here: https://gist.github.com/1323984
Most bizzarre simply calling map might be causing the problem.
puts "just created user with these groups:"
puts #user1.joined_groups.map(&:name)
puts "then secondly"
puts #user1.joined_groups.map(&:name)
Generates this (!):
just created user with these groups:
Dan Cole
CA
CA46
Gang of 13
then secondly
Dan Cole
CA
CA46
Gang of 13
Dan Cole
CA
CA46
Gang of 13
Thanks for any insight! After repeated attempts, I can't figure out a way in terminal to duplicate this, so I am suspecting the Fabrication gem. (Update: nope, I get this error with standard mongoid objects, so I am totally blaming mongoid.)
Tim
I think the problem might simply be that you are not pushing the groups onto the user correctly. Try using concat or separately shoveling them.
#user1.joined_groups.concat([common_group,
cali_group,
ca46,
Fabricate(:polco_group, {:name => "Gang of 13", :type => :custom})])