Rails Fixnum Error - ruby-on-rails

I have a simple query that Rails seems to be interpreting as a fixnum, but I'm not sure why. My code looks like this:
#user_with_points = Point.select("sum(points) as points, user_id").order("points desc").group("user_id")
#user_with_points.each_with_index do |user_with_point, index|
When I add puts #user_with_points, it shows:
#<Point:0x6360138>
#<Point:0x6322f38>
However, I'm receiving this error this error:
NoMethodError: undefined method 'each' for 75:Fixnum
adding Entire Code
def self.update_overall_rank_and_points
#user_with_points = Point.select("sum(points) as points, user_id").order("points desc").group("user_id")
rank = 0
points = 0
#user_with_points.each_with_index do |user_with_point, index|
#user = User.find(user_with_point.user_id)
if user_with_point.points != points
points = user_with_point.points
rank += 1
end
#user.rank = rank
#user.points = user_with_point.points
#user.save
end
end

Your query is returning a scalar value which the sum of points as an integer. The total of your query happens to be 75, hence the error. Therefore you can't do an each against it since it's not an enumeration.

Try:
#user_with_points = Point.sum(:points, :group => :user_id, :order => 'sum(points)')
#user_with_points.each do |user_id, points|
#...
user = User.find(user_id)
if user.points != points
puts "not equal!"
end
end

Related

Increment/decrement integer in user model by 1

I have a user model which has these columns - name, email, books_on_loan
I have a booking system where users can check out/in books. When A user checks a book out I want to increase their 'books_on_loan_ integer by one, and vice versa.
So far I have tried it like this and am getting an error "undefined method `+' for nil:NilClass" - it doesnt like the #user.books_on_loan
def check_out
#params = params[:book]
title = #params[:title]
name = #params[:name]
#book = Book.find_by_title(title)
#user = User.find_by_name(name)
if #user == nil
#note = 'This user is not registered.'
elsif #book == nil
#note = 'This book is not in the library.'
elsif #book.onloan == 1
#note = 'This book is on loan.'
elsif #user.books_on_loan == 3
#note = 'This user already has 3 books out.'
else
#book.onloan = 1
#book.save
#books_loaned = BooksOnloan.create(book_id: #book.id, user_id: #user.id)
#books_loaned.save
#user.books_on_loan = #user.books_on_loan + 1
#user.save
#note = 'The book was checked out.'
end
respond_to do |format|
format.html
end
end
Try this:
#user.increment :books_on_loan, 1
You can try with following codes, that will handle nil value for #user.books_on_loan
#user.books_on_loan = #user.books_on_loan.to_i + 1
or
#user.books_on_loan = (#user.books_on_loan || 0) + 1
or
#user.books_on_loan = (#user.books_on_loan.present? ? #user.books_on_loan : 0) + 1
While most answers here are ok, you'd rather change the root of the problem otherwise you'd have to use guard causes in your whole app.
It seems books_on_loan to be an attribute in db, so do :
change_column :users, :books_on_loan, :integer, default: 0
#and change existing bad data
User.update_all({books_on_loan: 0}, {books_on_loan: nil})
Or you could change the getter in your class:
def books_on_loan
super || 0
end
Side note, design wise, its not that a good idea to have an integer in db maintaining the current books on loan: you could loose sync the real ones.
You'd rather count a real association.
Simply do #user.books_on_loan.to_i. The to_i will convert nil into 0 automatically
ost answers are very correct but I'd like to add the following.
To answer your question, incrementing the value of an attribute (even on a variable that returns nil) can be done this way:
#user.books_on_loan =+ 1
decrementing being: #user.books_on_loan =- 1
However, you can leverage the benefit of proper model associations here and have a has_many -> through relationship between User and Book where by adding/removing a book for a user, the counter is adjusted accordingly.
Rails provides practical abstraction that you can use: increment and decrement.
So you could now do something like this:
#user.increment(:books_on_loan, 1)
Increment takes 2 argument. The table column and the number by which you increment. It initializes the attribute to zero if nil and adds the value passed as by (default is 1).
Similarly you can use
#user.decrement(:book_on_loan, 1)
More on this HERE

Ruby each block next when exception or error is raised

I have the following method in my rake task.
def call
orders = Spree::Order.complete.where('completed_at >= :last_day', last_day: Time.now - 30.days)
orders.each do |order|
order_tracking = order.shipments.first.tracking
next if order_tracking.nil?
shipment = order.shipments.first
results = fedex.track(tracking_number: order_tracking)
tracking_info = results.first
status = tracking_info.status.to_s
delivery_date = tracking_info.delivery_at
shipment.is_delivered = delivered?(status)
shipment.date_delivered = delivery_date
shipment.save
puts "-> Shipping status was updated for #{order.number}"
end
end
If there is an order with no tracking number I skipping it with next on line 5.
My question: How would I do next if a tracking number is invalid and the following error is raised:
Fedex::RateError: Invalid tracking number.
Ideally I would like to change line 5 to:
next if order_tracking.nil? || order_tracking.raised(Fedex::RateError) # something like that
Thank you in advance.
Also RateError is raised here:
def process_request
api_response = self.class.post(api_url, :body => build_xml)
puts api_response if #debug == true
response = parse_response(api_response)
if success?(response)
options = response[:track_reply][:track_details]
if response[:track_reply][:duplicate_waybill].downcase == 'true'
shipments = []
[options].flatten.map do |details|
options = {:tracking_number => #package_id, :uuid => details[:tracking_number_unique_identifier]}
shipments << Request::TrackingInformation.new(#credentials, options).process_request
end
shipments.flatten
else
[options].flatten.map do |details|
Fedex::TrackingInformation.new(details)
end
end
else
error_message = if response[:track_reply]
response[:track_reply][:notifications][:message]
else
"#{api_response["Fault"]["detail"]["fault"]["reason"]}\n--#{api_response["Fault"]["detail"]["fault"]["details"]["ValidationFailureDetail"]["message"].join("\n--")}"
end rescue $1
raise RateError, error_message
end
end
added:
private
def fedex_track(tracking)
fedex.track(tracking_number: tracking)
end
And changed results on line 7 to:
results = fedex_track(order_tracking) rescue next

Rails undefined method `+' for nil:NilClass

I have a method 'calc_price' that was working previously, and still works in console, but is now giving me the following error in a browser:
NoMethodError in Quotes#index
undefined method `+' for nil:NilClass
18: Price: $<%= f.calc_price %><br />
app/models/quote.rb:33:in `block in calc_price'
app/models/quote.rb:13:in `calc_price'
app/views/quotes/index.html.erb:18:in `block in _app_views_quotes_index_html_erb__1788058106025144185_70227449765940'
app/views/quotes/index.html.erb:15:in `each'
app/views/quotes/index.html.erb:15:in `_app_views_quotes_index_html_erb__1788058106025144185_70227449765940'
The fact that it still works in console is confusing to me, especially since I didn't change the method at all for it to break. The method:
def calc_price
self.items.each do |item|
pr = if item.amount < 10
item.product.pricerange0
elsif item.amount < 25
item.product.pricerange1
elsif item.amount < 50
item.product.pricerange2
elsif item.amount < 100
item.product.pricerange3
elsif item.amount < 250
item.product.pricerange4
elsif item.amount < 500
item.product.pricerange5
end
screens = 0
sd = item.shirtdesigns.count
pd = item.pantdesigns.count
screens = (sd+pd)
screenprice = (screens*25)
inkprice = ((item.inkcolors-1)*0.5)
newprice = ((pr+inkprice)*item.amount+screenprice)
item.price = newprice
item.save
end
newprice = self.items.sum('price')
self.price = newprice
self.save
return self.price
end
quote controller
def index
#quote = Quote.find(:all)
#myquotes = Quote.find(:all, :conditions => { :user_email => current_user.email })
end
I tried adding screenprice = 0, newprice = 0 and inkprice = 0 to see if that would make a difference but it did not.
If it still works in console does that mean maybe its not the method itself that is broken?
Any help would be greatly appreciated! Thanks
pr is most likely nil. An item with an amount greater than 500 would result in pr being nil in the code above.

Nested ActiveRecords: Find many childrens of many parents

In my Rails 3.2 app a Connector has_many Incidents.
To get all incidents of a certain connector I can do this:
(In console)
c = Connector.find(1) # c.class is Connector(id: integer, name: string, ...
i = c.incidents.all # all good, lists incidents of c
But how can I get all incidents of many connectors?
c = Connector.find(1,2) # works fine, but c.class is Array
i = c.incidents.all #=> NoMethodError: undefined method `incidents' for #<Array:0x4cc15e0>
Should be easy! But I don't get it!
Here’s the complete code in my statistics_controller.rb
class StatisticsController < ApplicationController
def index
#connectors = Connector.scoped
if params['connector_tokens']
logger.debug "Following tokens are given: #{params['connector_tokens']}"
#connectors = #connectors.find_all_by_name(params[:connector_tokens].split(','))
end
#start_at = params[:start_at] || 4.weeks.ago.beginning_of_week
#end_at = params[:end_at] || Time.now
##time_line_data = Incident.time_line_data( #start_at, #end_at, 10) #=> That works, but doesn’t limit the result to given connectors
#time_line_data = #connectors.incidents.time_line_data( #start_at, #end_at, 10) #=> undefined method `incidents' for #<ActiveRecord::Relation:0x3f643c8>
respond_to do |format|
format.html # index.html.haml
end
end
end
Edit with reference to first 3 answers below:
Great! With code below I get an array with all incidents of given connectors.
c = Connector.find(1,2)
i = c.map(&:incidents.all).flatten
But idealy I'd like to get an Active Records object instead of the array, because I'd like to call where() on it as you can see in methode time_line_data below.
I could reach my goal with the array, but I would need to change the whole strategy...
This is my time_line_data() in Incidents Model models/incidents.rb
def self.time_line_data(start_at = 8.weeks.ago, end_at = Time.now, lim = 10)
total = {}
rickshaw = []
arr = []
inc = where(created_at: start_at.to_time.beginning_of_day..end_at.to_time.end_of_day)
# create a hash, number of incidents per day, with day as key
inc.each do |i|
if total[i.created_at.to_date].to_i > 0
total[i.created_at.to_date] += 1
else
total[i.created_at.to_date] = 1
end
end
# create a hash with all days in given timeframe, number of incidents per day, date as key and 0 as value if no incident is in database for this day
(start_at.to_date..end_at.to_date).each do |date|
js_timestamp = date.to_time.to_i
if total[date].to_i > 0
arr.push([js_timestamp, total[date]])
rickshaw.push({x: js_timestamp, y: total[date]})
else
arr.push([js_timestamp, 0])
rickshaw.push({x: js_timestamp, y: 0})
end
end
{ :start_at => start_at,
:end_at => end_at,
:series => rickshaw #arr
}
end
As you only seem to be interested in the time line data you can further expand the map examples given before e.g.:
#time_line_data = #connectors.map do |connector|
connector.incidents.map do |incident|
incident.time_line_data(#start_at, #end_at, 10)
end
end
This will map/collect all the return values of the time_line_data method call on all the incidents in the collection of connectors.
Ref:- map
c = Connector.find(1,2)
i = c.map(&:incidents.all).flatten

Slice/Map Ordered Hash

I am writing a "Punch Clock" application for my office.. I am working on the controller for the "TIme Card" view which should list a users punches for a given week, and total DAILY then add the TOTAL for the week. I have figured out how to get the time diff between all of the punches with slice/map, my issue is that when I try to do this on the ordered hash (grouped by days) I get undefined method `created_at' for #, I know I must be missing some syntax somewhere, your help is greatly appreciated...
Here is my controller...
Note that if i call #in_out_lenghts on #punches, this works and gives me the total for the week, but #punches_days gives me an error, therefore I can not keep a running tally....
def mytimecard
#week = params[:week].to_s
if #week == "lastweek"
#punches = Punch.lastweek.where("user_id = ?", params[:user])
else
#punches = Punch.thisweek.where("user_id = ?", params[:user])
end
#punches_days = #punches.group_by { |t| t.created_at.beginning_of_day}
if #punches.count%2 == 0
#in_out_lengths = #punches_days.each_slice(2).map { |a|(a[1].created_at).round(15.minutes) - (a[0].created_at).round(15.minutes) }
#total = ((#in_out_lengths.inject(:+))/60/60)
else
#total = "Can Not Calculate, Odd Number of Punches"
end
respond_to do |format|
format.html # timecard.html.erb
format.json { render :json => #punches }
end
end
group_by will return a hash of days and punches.
{ :day_1 => [ :punch1, :punch2], :day_2 => [ :punch3, :punch4, :punch5 ] }
doing an each_slice and a map will result in some sort of array, but probably not what you meant.
You may have meant to count the number of punches and call something like this
Punch.lastweek.where("user_id = ?", params[:user]).group('date(created_at)')
This would have resulted in the date => punches_count format, at least with mysql.

Resources