undefined method `days_in_a_month' for Time:Class [closed] - ruby-on-rails

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I tried several things and I can't seem to find the reason why Time::days_in_a_month is not found.
I'm trying to calculate the amount of signups for each day of the year, this is the code:
require 'date'
require 'time'
class GraphicsController < ApplicationController
def year_graph(year=2013)
data=calculate_signups(year)
gon.data=data
end
def calculate_signups(year)
#binding.pry
month_days=(1..12).map{|x| Time.days_in_a_month(x,year)}
total_yeardays=month_days.sum
year_sum=Array.new
day_sum={day:0,yearday:0,num:0}
afee=User.clients.year(year)#.month(month)
month_days.each_with_index do |mindex,mdays|
(1..mdays).each do |d|
day_fees=afee..month(mindex).day(d)
dia=day_sum.dup
suma=day_fees.length
dia[:day]=d
dia[:num]=suma
dia[:yearday]=Date.new(year,mindex,d).yday
year_sum << dia
end
end
year_sum
end
end
If I try it in the debugger pry(see binding.pry line) there is no problem. And of course no problem either when I try it on the console rails c --sandbox. I assume I'm missing something, but I don't know what!

The correct syntax is without a. The correct is
Time.days_in_month

Related

How to use or in checking presence? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i need to check presence of a value in an array. It can be two values i need to check with an or case if it includes in the array. I think it will be confusing from my description you will understand after you see the code below.
if user.sheet_ids.include? params[:id].to_i || params[:sheet_id].to_i
can :show, Project
end
It returns false even if sheet_id is present in the array of sheet_ids.
user.sheet_ids is an array and i need to check if any of the value includes in this array. The method i used is not seems to be working.
Is there any simple way of doing this. Thanks.
I would do something like this:
(user.sheet_ids & params.values_at(:id, :sheet_id).map(&:to_i)).any?
user.sheet_ids.include? params[:id].to_i || params[:sheet_id] is executed in this way user.sheet_ids.include?(params[:id].to_i || params[:sheet_id]). The OR statement is executed and the result is what is passed as an argument to user.sheet_ids.include?
To check if either params[:id].to_i or params[:sheet_id] is included in the array users.sheet_ids, you need to do this;
if user.sheet_ids.include?(params[:id].to_i) || user.sheet_ids.include?(params[:sheet_id].to_i)
can :show, Project
end

ruby on rails beginner: rails for zombies code sample [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
where does that #user in the code in check_ammo come from?
see code for "weapon" in model, controller, mailer and schema.rb
rails for zombies code
That code cant be right.
Weaponmailer
def low_ammo(weapon, zombie)
attachments["weapon.jpg"] = weapon.picture_file
mail to: zombie.email, subject: "#{weapon.name} has low ammo"
end
expects 2 parameters in controller
WeaponMailer.low_ammo(#user).deliver
#user is not defined unless its defined in ActiveRecord::Base what is not the case i think. Its just broken code example

Why isn't this route working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the following route to display the cars of a user:
get 'cars/id:/all' => 'wikipages#index', as: 'cars_index'
This translates to:
cars_index_path GET /cars/id:/all(.:format) cars#index
However, when I visit site-url/cars/1/all I get the error No route matches. Am I visiting the route/path incorrectly?
P.S. Controller method:
def index
#user = User.find(params[:id])
#cars = #user.garage.cars.all
end
Variable segments use a leading :, not a trailing :.
You need :id, not id:.

How to get a ruby class when it's name is in a variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
So I'm passing a string with the name of an activerecord model class into my method and I want to be able to call some methods on that class.
This is wrong:
def perform(body_class, body_id)
body = body_class.constantize
request = body.find(body_id)
end
Update - that actually works fine.
What error are you getting? Make sure the string is in the correct ActiveRecord format.
For example:
"Person".constantize # works
"person".constantize # does not work
To get the second one to work you should also add camelize
"person".camelize.constantize # works

What does this controller code do? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've come across this pattern a few times, but am not sure what it is, or how to Google it.
class ApiController < ApplicationController
class InvalidAppToken < RuntimeError ; end
class InvalidUserToken < RuntimeError ; end
...
end
It creates custom errors. Doing this you can raise your personalized errors, then rescue them to better handle the app errors.
class MyController < ApiController
begin
unless DoSomething(params[:user_token])
raise InvalidUserToken
[...]
end
rescue InvalidUserToken
# manage this error
end
end
Here is a good option for searching the web with special symbols considered (most major search engines ignore special characters in searches):
http://symbolhound.com/?q=ruby+%3C+runtimeerror
It simply defines custom types of errors. Could be useful to raise very specific errors in your workflow.
Have a look at this book

Resources