ActionView::Template::Error (THING can't be coerced into Fixnum): - ruby-on-rails

Can anyone explain this error? Trying to divide and subtract in an ERB file. I am sure best practice is to do it in the controller, but would rather not.
ActionView::Template::Error (AcctLedger can't be coerced into Fixnum):
Mar 09 00:25:34 myapp app/web.2: 107:
Mar 09 00:25:34 myapp app/web.2: 108: <% if "myapp XL" == call.requested_type %>
Mar 09 00:25:34 myapp app/web.2: 109: <% if call.has_fee? %>
Mar 09 00:25:34 myapp app/web.2: 110: <% price = (price - call.has_fee)/2 %>
Mar 09 00:25:34 myapp app/web.2: 111: <% else %>
Mar 09 00:25:34 myapp app/web.2: 112: <% price = price/2 %>
Mar 09 00:25:34 myapp app/web.2: 113: <% end %>

Is price or call.has_fee returning an AcctLedger object, by any chance? This error is saying that AcctLedger has no idea how to act like a number, but you're trying to use it in a calculation.
You can see a similar thing if you try to add a number and a string:
1 + '1'
# TypeError: String can't be coerced into Fixnum

Related

How to join points by line?

I am using chartkick and gem "groupdate" in my project.
<%= area_chart SugarLevel.group_by_minute(created_at).sum(:mmol) %>
I want to keep continuing line from one point to another. I do something wrong with groupdate. If there is no data at some minute, the line has to start from previous point and has to finish at next point.
It has to be graph without gaps. Like that:
Method in gem "groupdate" group_by_something uses Hash to store a data. For example:
#hash = SugarLevel.group_by_minute(:created_at).sum(:mmol) # => {Thu, 01 Jun 2017 17:39:00 UTC +00:00=>#<BigDecimal:2add9a8,'0.77E1',18(18)>, Thu, 01 Jun 2017 17:40:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:41:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:42:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:43:00 UTC +00:00=>0}
We can clear this hash as we want:
#hash.select{|k, v| v != 0} # Select all keys which do not consist 0
And eventually, we can put itin a view: <%= area_chart #hash %>

Comparing datetime in Rails always false

I'm trying to compare two dates of which both are datetime.
closed_at and updated_at (while updated_at is the one already created by rails)
This is my code:
<% if #task.closed_at == #task.updated_at %>
*Some stuff*
<% end %>
With closing a task it gets automatically updated.
irb(main):011:0> Task.first.closed_at
=> Fri, 07 Aug 2015 10:47:58 CEST +02:00
irb(main):012:0> Task.first.updated_at
=> Fri, 07 Aug 2015 10:47:58 CEST +02:00
irb(main):013:0> Task.first.closed_at == Task.first.updated_at
=> false
irb(main):014:0> Task.first.closed_at.to_datetime == Task.first.updated_at.to_datetime
=> false
I've read this two posts:
comparing datetime and time produces a false result
DateTime comparison fails in Rails
But neither the data type is different nor the time zone.
Environment:
ruby 2.2.2p95
rails 4.2.3
pg 0.18.2
This can happen because ruby Time makes comparison with fractions of seconds. So the "print" may look equal, but there is some time difference between both dates.
Try to use to_f method to see the difference between #task.closed_at and #task.updated_at. Pretty sure is this issue.
To solve your problem, just ignore the milliseconds part of dates:
<% if #task.closed_at.to_i == #task.updated_at.to_i %>
*Some stuff*
<% end %>
Try this little experiment with any model that has a created_at and updated_at in the console:
user = User.new
user.created_at = 'Fri, 07 Aug 2015 10:47:58 CEST +02:00'.to_datetime
user.updated_at = 'Fri, 07 Aug 2015 10:47:58 CEST +02:00'.to_datetime
user.created_at == user.updated_at
The last statement above returns true. So there has to be a difference in the time not visible when the time is displayed.
It happens because of the little difference (in milliseconds) between those 2 variables. There is one blog post regarding this issue:
http://railsware.com/blog/2014/04/01/time-comparison-in-ruby/
Basically, an easy fix would be to convert the time values to int:
<% if #task.closed_at.to_i == #task.updated_at.to_i %>
*Some stuff*
<% end %>

Having trouble pulling X-Mailer header flags from an email even though request extra header is set

I'm at my wits end trying to pull the "X-Mailer" header from an email. All other seems to work fine. Any ideas on how to debug / what I might possibly be doing wrong?
Here's what I'm trying:
message.header.allExtraHeadersNames; => ()
message.header.userAgent; => "(null)"
[message.header extraHeaderValueForName:#"X-Mailer"] => "(null)
I'm requesting these headers:
return (MCOIMAPMessagesRequestKind)
(MCOIMAPMessagesRequestKindHeaders | MCOIMAPMessagesRequestKindStructure |
MCOIMAPMessagesRequestKindInternalDate | MCOIMAPMessagesRequestKindHeaderSubject |
MCOIMAPMessagesRequestKindGmailThreadID | MCOIMAPMessagesRequestKindFullHeaders |
MCOIMAPMessagesRequestKindGmailLabels | MCOIMAPMessagesRequestKindExtraHeaders |
MCOIMAPMessagesRequestKindFlags);
I can validate that the correct RequestKindHeaders is being sent as I am able to pull the correct GmailThreadID:
message.gmailThreadID; => 1504802571269374502
And the email header as per Gmail's "original message".
Delivered-To: vu0tran#xxxxx.com
Received: by 10.70.126.36 with SMTP id mv4csp3102740pdb;
Tue, 23 Jun 2015 14:25:31 -0700 (PDT)
X-Received: by 10.13.219.13 with SMTP id d13mr6638173ywe.22.1435094730675;
Tue, 23 Jun 2015 14:25:30 -0700 (PDT)
Return-Path: <bounce-mc.us6_12976611.827941-vu0tran=gmail.com#mail84.atl31.mcdlv.net>
Received: from mail84.atl31.mcdlv.net (mail84.atl31.mcdlv.net. [205.201.134.84])
by mx.google.com with ESMTP id e184si9259660ywa.76.2015.06.23.14.25.29
for <vu0tran#gmail.com>;
Tue, 23 Jun 2015 14:25:29 -0700 (PDT)
Received-SPF: pass (google.com: domain of bounce-mc.us6_12976611.827941-vu0tran=gmail.com#mail84.atl31.mcdlv.net designates 205.201.134.84 as permitted sender) client-ip=205.201.134.84;
Authentication-Results: mx.google.com;
spf=pass (google.com: domain of bounce-mc.us6_12976611.827941-vu0tran=gmail.com#mail84.atl31.mcdlv.net designates 205.201.134.84 as permitted sender) smtp.mail=bounce-mc.us6_12976611.827941-vu0tran=gmail.com#mail84.atl31.mcdlv.net;
dkim=pass header.i=#mail84.atl31.mcdlv.net
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; s=k1; d=mail84.atl31.mcdlv.net;
h=From:Reply-To:To:Date:Message-ID:List-ID:List-Unsubscribe:Sender:Subject:Content-Type; i=vu=XXXXXXXX.com#mail84.atl31.mcdlv.net;
bh=rAP8hOadQtBeDm6jm9/SrnV0Y/o=;
b=pp/7bQcQFZXsajvEDHf+faCrkOg95UyrcpZ1NUVzt3O5dxSWmjhefcytwvQiVbcw8FIyyekpZMke
ezAjhCRqZKyM0xafVQGtqVX2jKmDsgyA5r0cV2FBa8ObFX+mMjHRDYVzXrW+QfKlrc9buaNZwv9Q
VZVWotIxzZKHn9Qu8gE=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; s=k1; d=gmail.mcsv.net;
h=From:Reply-To:To:Date:Message-ID:X-Feedback-ID:List-ID:List-Unsubscribe:Sender:Subject:Content-Type;
bh=rAP8hOadQtBeDm6jm9/SrnV0Y/o=;
b=l5yRKHMnZEW1slk+VOypUZ3r/24fJjff022IE2p9fiOUDBIx+UiHYdWyMdt6jhH85UDDOEHstnDe
OYBJfj80RXqduVRvnaSRDRtZV1W3PCZFLfoLcAS72V0cCHaRZBBojIwlOJFcmXKdBM2TxC5D8yOq
7t4fUwaXmIsLYl6eovg=
DomainKey-Signature: a=rsa-sha1; c=nofws; q=dns; s=k1; d=mail84.atl31.mcdlv.net;
b=nLYfHDRQzNRmbnWc8tnSDAZtiWu4E+aGOZBFBUVz44vR+I47xEpuY4ac0KFL+Uc01K66tVX+kj+9
EbocAToElbAndJ6WD6YYnkgBdKz03OaANjesAvwIPe1jH/+Zqfw3eQUHGGTJuYKsPD6a4f+sldll
YNnpCr6MUwStCDm6obU=;
Received: from (127.0.0.1) by mail84.atl31.mcdlv.net id hh77ci1mqrog for <vu0tran#gmail.com>; Tue, 23 Jun 2015 21:25:29 +0000 (envelope-from <bounce-mc.us6_12976611.827941-vu0tran=gmail.com#mail84.atl31.mcdlv.net>)
From: =?utf-8?Q?XXXXXXX?= <vu#XXXXXX.com>
Reply-To: <us6-40dbc13c7a-bf64292c7d#inbound.mailchimp.com>
To: =?utf-8?Q?=3C=3C=20Test=20First=20Name=20=3E=3E?= <vu0tran#gmail.com>
Date: Tue, 23 Jun 2015 21:25:29 +0000
Message-ID: <afe03fcb279af0d81939903e7.20150623212521#mail84.atl31.mcdlv.net>
X-Mailer: MailChimp Mailer - **CID31045d1dc9**
X-Campaign: mailchimpafe03fcb279af0d81939903e7.31045d1dc9
X-campaignid: mailchimpafe03fcb279af0d81939903e7.31045d1dc9
X-Report-Abuse: Please report abuse for this campaign here: http://www.mailchimp.com/abuse/abuse.phtml?u=afe03fcb279af0d81939903e7&id=31045d1dc9&e=
X-MC-User: afe03fcb279af0d81939903e7
X-Feedback-ID: 12976611:12976611.827941:us6:mc
List-ID: afe03fcb279af0d81939903e7mc list <afe03fcb279af0d81939903e7.121893.list-id.mcsv.net>
X-Accounttype: ff
List-Unsubscribe: <mailto:unsubscribe-afe03fcb279af0d81939903e7-31045d1dc9-#mailin1.us2.mcsv.net?subject=unsubscribe>, <http://vutran.us6.list-manage.com/unsubscribe?u=afe03fcb279af0d81939903e7&id=fe48c8ecec&e=&c=31045d1dc9>
Sender: "XXXXXX" <vu=XXXXXX.com#mail84.atl31.mcdlv.net>
x-istest: yes
Subject: =?utf-8?Q?=5BTest=5D=20Introducing=20email=20filtering?=
x-mcda: TRUE
Content-Type: text/plain; charset="us-ascii"

ActiveSupport::JSON::Encoding::CircularReferenceError in content_tag

I have my User model with the following functions:
def self.chart_data(start = 1.weeks.ago)
total_users = users_by_day(start)
(start.to_date..Date.today).map do |date|
{
created_at: date,
users: total_users[date] || 0
}
end
end
def self.users_by_day(start)
users = where(created_at: start.beginning_of_day..Time.zone.now)
users = users.group("date(created_at), id")
users = users.select("created_at, count(id) as total_users")
users.each_with_object({}) do |user, total_users|
total_users[user.created_at.to_date] = total_users
end
end
And my View has the following line:
<%= content_tag :div, "", id: "users_chart", data: {users: User.chart_data} %>
I get the following error:
ActiveSupport::JSON::Encoding::CircularReferenceError in Static#statistics
Showing statistics.html.erb where line #5 raised:
object references itself
When i i just print the data it looks as the following:
[{:created_at=>Wed, 27 Nov 2013, :users=>0}, {:created_at=>Thu, 28 Nov 2013, :users=>0}, {:created_at=>Fri, 29 Nov 2013, :users=>0}, {:created_at=>Sat, 30 Nov 2013, :users=>0}, {:created_at=>Sun, 01 Dec 2013, :users=>0}, {:created_at=>Mon, 02 Dec 2013, :users=>0}, {:created_at=>Tue, 03 Dec 2013, :users=>{Tue, 03 Dec 2013=>{...}}}, {:created_at=>Wed, 04 Dec 2013, :users=>0}]
The only user is on 03 Dec, if there are no users there is also no error.
As you'll note in the printed data, there is a recursive reference on the 3rd:
{:created_at=>Tue, 03 Dec 2013, :users=>{Tue, 03 Dec 2013=>{...}}}
The origins come from how users_by_day is constructed. The has building references itself, by assigning an object on a given day to itself:
users.each_with_object({}) do |user, total_users|
total_users[user.created_at.to_date] = total_users
end
I think this is just an oversight in variable naming. The aggregate object you are creating is called total_users by the block, and it's the same name you give it inside the query. I believe the correct block would be this:
users.each_with_object({}) do |user, total_users|
total_users[user.created_at.to_date] = user.total_users
end
Since you need to get the total off the user variable.

undefined method `strftime_format'

I'm use gems stamp and StampI18n.
Code:
#post.time = #date.localize_stamp("1 Jan 1999, 21:45", :locale => 'en')
Result:
undefined method `strftime_format' for Thu, 09 Jun 2011:Date
What's the problem?

Resources