How to use or in checking presence? [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 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

Related

How to find out the number of records in a table? [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 3 years ago.
Improve this question
I have a scaffold help requisition. There is a status field. Each request can have 3 statuses (pending, in_progress, finished), implemented through enum. It is necessary to write methods that will count the number of requests with each status. For example, method A counts how many total helper prequests with status 0 (pending). I understand that there is nothing complicated about this, but I can't understand and could use some help.
It would be something like this on your model:
def self.pending_count
where(status: 0).count
end
def self.in_progress_count
where(status: 1).count
end
def self.finished_count
where(status: 2).count
end
Then you can just call Model.pending_count (or the other methods) wherever you need it.

Deep Rails JSON parse [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 5 years ago.
Improve this question
Is it possible to have JSON.parse work 2 layers deep so that a hash within a hash will get parsed as well? Is there a method for or do I have to do something like JSON.parse(JSON.parse(...)['foo'])?
JSON.parse doesn't care about your hash structure:
> str = JSON.dump({foo: {bar: {baz: :qux}}})
=> "{\"foo\":{\"bar\":{\"baz\":\"qux\"}}}"
> p = JSON.parse(str).with_indifferent_access
=> {"foo"=>{"bar"=>{"baz"=>"qux"}}}
> p[:foo][:bar][:baz]
=> "qux"
(Well, it cares if you have a malformed string, but that's something else altogether.)
So, what are you asking?

Resolve Fixnum error [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 6 years ago.
Improve this question
I have a parameter (params[:authorization]) that comes from the URL, as you can see:
authorization_selected = params[:authorization]
new_parcel = params[:new_parcel].to_i
puts authorization_selected.class (in the console show type String)
puts new_parcel.class (in the console show type Fixnum)
In my controller, have:
#portability = Portability.new
#portability.employee_id = authorization_selected.employee_id
However this returns an error:
undefined method `employee_id' for 3:Fixnum
I need that both was integer. How do it?
You are calling the employee_id method on authorization_selectedwhich is a String and does not provide this method.
Obviously this does not work. You probably want to do
#portability = Portability.new
#portability.employee_id = authorization_selected
assuming that params[:employee] contains the employee_id and Portability is an ActiveModel or an ActiveRecord.
Perhaps you can change your form that the value can be assigned through the initializer?

undefined method `days_in_a_month' for Time:Class [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
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

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

Resources