Ruby array - comparison customer_id with an array - ruby-on-rails

I have array of numbers which means governings to acces customer:
Controller :
#user_got_these_governings = current_user.governings.map{ |governing| governing.customer_id}
which_customer_is_selected = params[:user][:customer_id]
i need to compare customer_id with an array of numbers:
customer_id: 1
Governings: [7,9,6,2,3,1]
if Governings match with customer_id = true
if not = false

Something like
which_customer_is_selected = #user_got_these_governings.include?(params[:user][:customer_id].to_i)
to_i because you are probably receiving a string in the parameter and you are comparing it with an array of integers.

I think that cleanest way to do it is use in? method.
governings = [7,9,6,2,3,1]
params[:user][:customer_id].to_i.in? governings
=> true

Another possible way to do it is by using .member? method which tells whether an element is a member of the array or not.
Governings: [7,9,6,2,3,1]
Governings.member? params[:user][:customer_id].to_i
=> true

Related

Ruby add nil value to an array

How can I initialize an array and be able to add nil value to it? as I know Array.wrap doesn't do the job.
list = Array.wrap(nil) => []
What I want:
list = Array.add(nil) => [nil]
Thank you
Try:
list = Array.new(1)
The number fed in as an argument dictates how many nils are added:
list = Array.new(3)
=> [nil, nil, nil]
Maybe you are looking for (Rails):
list = Array.wrap([nil])
#=> [nil]
But why not just a simple list = [nil], as per #engineersmnky's comment?
Also list = Array.new.push nil, but it's still better the easy way above.

Check if string contains element in Array

I'm using Rails and learning ActiveRecord and I came across a vexing problem. Here's an array in my model:
#sea_countries = ['Singapore','Malaysia','Indonesia', 'Vietnam', 'Philippines', 'Thailand']
And here's my ActiveRecord object:
#sea_funding = StartupFunding.joins(:startup)
.where('startups.locations LIKE ?', '%Singapore%')
What I'm trying to do is to return a result where a string in the 'locations' column matches any element in the Array. I'm able to match the strings to each element of an Array (as above), but I'm not sure how to iterate over the whole Array such that the element is included as long as there's one match.
The intent is that an element with multiple locations 'Singapore,Malaysia' would be included within #sea_funding as well.
Well, don't ask me why 'locations' is set as a string. It's just the way the previous developer did it.
You use an IN clause in your .where filter:
#sea_funding = StartupFunding.joins(:startup)
.where(["startups.locations IN (?)", #sea_countries])
#sea_countries.include?(startups.locations)
This will return a boolean TRUE if the value of the locations column in startups can be found in the sea_countries array, false if it is absent.
Could this work for you?
first = true
where_clause = nil
sea_countries.each do |country|
quoted_country = ActiveRecord::Base.connection.quote_string(country)
if first
where_clause = "startups.locations LIKE '%#{quoted_country}%' "
first = false
else
where_clause += "OR startups.locations LIKE '%#{quoted_country}%' "
end
end
#sea_funding = StartupFunding.joins(:startup)
.where(where_clause)

Storing and retrieving session objects

I'm storing an object in hash which is in session object like this :
hash_key = ImportantData.new
hash_key.test_id = params[:test_id]
hash_key.user_id = params[:user_id]
session[:important_data] ||= {}
session[:important_data][hash_key] = nil
And then I print this map session[:important_data][hash_key] in my other controller and try to check whether the object is in there or not like this :
hash_key = ImportantData.new
hash_key.schedule_id = #test.id
hash_key.user_id = #user.id
in_hash = session[:important_data].has_key?(hash_key) unless session[:important_data].nil?
in_hash is always false to me, what am I doing wrong? and is there a better way to do this?
In Ruby, hash keys work by equality. You can store and retrieve a value in a hash as long as the key you're using is equal to the key that's stored.
For instance:
hsh = { 'hello' => 'world' }
p hsh['hello'] #=> "world"
'hello'.eql? 'hello' #=> true
You can retrieve the value because the same value string is always eql? in Ruby.
This is not the case for most objects:
class Example; end
object1 = Example.new
object2 = Example.new
object1.eql? object2 #=> false
Therefore, the key that you use to store in the hash must be eql? to the one that you're using to retrieve. eql? is equivalent to == and equal?.
You're creating two instances of ImportantData, which will not be equal to each other. It looks like you can accomplish what you want with a single hash key:
hash_value = ImportantData.new
hash_value.test_id = params[:test_id]
hash_value.user_id = params[:user_id]
session[:important_data] ||= hash_value
puts session[:important_data].class.name #=> ImportantData
session[:important_data].test_id #=> puts out value of test_id
I think you should assign it like this
session[:important_data][:hash_key] = hash_key
and access it
session[:important_data][:hash_key]

array to mongoid criteria

This query return an Array on users variable:
users = #users.flat_map {|b| b.followees_by_type('aged') }
I need apply this filter to users:
olds = users.any_of({ :image_filename.ne => nil }, { :yt_video_id.ne => nil}).all_of(:active.ne => false)
But I can not apply because is an Array.
Is possible change to mongoid criteria this array?
Any other solution?
Note important! I can not modify output class type b.followees_by_type('aged')
As per my comment, you should use #users which is a Mongoid::Criteria instead of users which is just an Array.

returning an array that contains an array and hash in ruby method?

HI
is it possible to return an array that contains an array and hash from a method in ruby?
i.e
def something
array_new = [another_thing, another_thing_2]
hash_map = get_hash()
return [array_new, hash_map]
end
and to retrieve the array:
some_array, some_hash = something()
thanks
Sure, that's perfectly possible and works exactly as in your example.
You will only ever be able to return one thing. What you are returning there is an array containing an array and a hash.
Ruby methods can be treated as if they return multiple values so you can collect the items in an array or return them as separate objects.
def something
array_new = Array.new
hash_new = Hash.new
return array_new, hash_new
end
a, b = something
a.class # Array
b.class # Hash
c = something
c.class # Array

Resources