Ruby add nil value to an array - ruby-on-rails

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.

Related

Ruby array - comparison customer_id with an array

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

Relation passed to #or must be structurally compatible. Incompatible values: [:references]

I have two queries, I need an or between them, i.e. I want results that are returned by either the first or the second query.
First query is a simple where() which gets all available items.
#items = #items.where(available: true)
Second includes a join() and gives the current user's items.
#items =
#items
.joins(:orders)
.where(orders: { user_id: current_user.id})
I tried to combine these with Rails' or() method in various forms, including:
#items =
#items
.joins(:orders)
.where(orders: { user_id: current_user.id})
.or(
#items
.joins(:orders)
.where(available: true)
)
But I keep running into this error and I'm not sure how to fix it.
Relation passed to #or must be structurally compatible. Incompatible values: [:references]
There is a known issue about it on Github.
According to this comment you might want to override the structurally_incompatible_values_for_or to overcome the issue:
def structurally_incompatible_values_for_or(other)
Relation::SINGLE_VALUE_METHODS.reject { |m| send("#{m}_value") == other.send("#{m}_value") } +
(Relation::MULTI_VALUE_METHODS - [:eager_load, :references, :extending]).reject { |m| send("#{m}_values") == other.send("#{m}_values") } +
(Relation::CLAUSE_METHODS - [:having, :where]).reject { |m| send("#{m}_clause") == other.send("#{m}_clause") }
end
Also there is always an option to use SQL:
#items
.joins(:orders)
.where("orders.user_id = ? OR items.available = true", current_user.id)
You can write the query in this good old way to avoid error
#items = #items.joins(:orders).where("items.available = ? OR orders.user_id = ?", true, current_user.id)
Hope that helps!
Hacky workaround: do all your .joins after the .or. This hides the offending .joins from the checker. That is, convert the code in the original question to...
#items =
#items
.where(orders: { user_id: current_user.id})
.or(
#items
.where(available: true)
)
.joins(:orders) # sneaky, but works! šŸ˜ˆ
More generally, the following two lines will both fail
A.joins(:b).where(bs: b_query).or(A.where(query)) # error! šŸ˜ž
A.where(query).or(A.joins(:b).where(bs: b_query)) # error! šŸ˜ž
but rearrange as follows, and you can evade the checker:
A.where(query).or(A.where(bs: b_query)).joins(:b) # works šŸ˜ˆ
This works because all the checking happens inside the .or() method. It's blissfully unaware of shennanigans on its downstream results.
One downside of course is it doesn't read as nicely.
I ran into the same issue, however the code was defined in a different place and was very difficult to change directly.
# I can't change "p"
p = Post.where('1 = 1').distinct # this could also be a join
And I needed to add an or statement to it
p.or(Post.where('2 = 2'))
The following code won't raise an error, because it has distinct like the initial relationship.
p.or(Post.where('2 = 2').distinct)
The problem with it it that it only works as long as you know the relationship. It may or not have a join, or distinct.
This works regardless of what the relationship is:
p.or(p.unscope(:where).where('2 = 2'))
=> SELECT DISTINCT `posts`.* FROM `posts` WHERE ((1 = 1) OR (2 = 2))
It occurs when you try to combine two multi-active records of the same type, but one of them has a joins value or an includes value, or in your case a reference value, that the other does not.
Therefore we need to match the values between them, and I found a general way to do this without knowing the actual values in advance.
items_1 = #items.joins(:orders)
.where(orders: { user_id: current_user.id})
items_2 = #items.where(available: true)
.joins(items_1.joins_values)
.includes(items_1.includes_values)
.references(items_1.references_values)
#items = items_1.or(items_2)
just solve it!
def exec_or_statement(q1, q2)
klass = q1.klass
key = klass.primary_key
query_wrapper_1 = {}
query_wrapper_1[key] = q1
query_wrapper_2 = {}
query_wrapper_2[key] = q2
klass.where(query_wrapper_1).or(klass.where(query_wrapper_2))
end
query_1 = #items.where(available: true)
query_2 =
#items
.joins(:orders)
.where(orders: { user_id: current_user.id})
exec_or_statement(query_1, query_2)

How do I find the first elelment in my Ruby array matching certain criteria?

Iā€™m using Rails 4.2.7. I want to find the first item in my array of objects whose fields match certain criteria. So I wrote this lengthy loop ā€¦
result = nil
results.each do |r|
if r.valid?
result = r
break
end
end
My question is, is there a shorter way to do this?
Yup there is:
result = results.find(&:valid?)
https://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-find
Thanks to Rashmirathi for the ampersand + colon shortcut!
You can try to do that using Array method select
results = [{id: 1, valid: true}, {id: 2, valid: false}, {id:3, valid: true}]
result = results.select { |item| item[:valid] == true}.first
You can find more at array documenation: https://ruby-doc.org/core-2.2.0/Array.html#method-i-select

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.

Ruby on Rails: Array to Hash with (key, array of values)

Lets say I have an Array of content_categories (content_categories = user.content_categories)
I now want to add every element belonging to a certain categorie to content_categories with the category as a key and the the content-item IDs as elements of a set
In PHP something like this is possible:
foreach ($content_categories as $key => $category) {
$contentsByCategoryIDArray = Category.getContents($category[id])
$content_categories[$key][$contentsByCategoryIDArray]
}
Is there an easy way in rails to do this?
Greets,
Nico
Your question isn't really a Rails question, it's a general Ruby programming question.
Your description isn't very clear, but from what I understand, you want to group IDs for common categories using a Hash. There are various other ways of doing this, but this is easy to understand::
ary = [
'cat1', {:id => 1},
'cat2', {:id => 2},
'cat1', {:id => 3}
]
hsh = {}
ary.each_slice(2) { |a|
key,category = a
hsh[key] ? hsh[key] << category[:id] : hsh[key] = [category[:id]]
}
hsh # => {"cat1"=>[1, 3], "cat2"=>[2]}
I'm using a simple Array with a category, followed by a simple hash representing some object instance, because it makes it easy to visualize. If you have a more complex object, replace the hash entries with those objects, and tweak how you access the ID in the ternary (?:) line.
Using Enumerable.inject():
hsh = ary.each_slice(2).inject({}) { |h,a|
key,category = a
h[key] ? h[key] << category[:id] : h[key] = [category[:id]]
h
}
hsh # => {"cat1"=>[1, 3], "cat2"=>[2]}
Enumerable.group_by() could probably shrink it even more, but my brain is fading.
I'd use Enumerable#inject
content_categories = content_categories_array.inject({}){ |memo, category| memo[category] = Category.get_contents(category); memo }
Hash[content_categories.map{|cat|
[cat, Category.get_contents(cat)]
}]
Not really the right answer, because you want IDs in your array, but I post it anyway, because it's nice and short, and you might actually get away with it:
content_categories.group_by(&:category)
content_categories.each do |k,v|
content_categories[k] = Category.getContents(v)
end
I suppose it's works
If i understand correctly, content_categories is an array of categories, which needs to be turned into a hash of categories, and their elements.
content_categories_array = content_categories
content_categories_hash = {}
content_categories_array.each do |category|
content_categories_hash[category] = Category.get_contents(category)
end
content_categories = content_categories_hash
That is the long version, which you can also write like
content_categories = {}.tap do |hash|
content_categories.each { |category| hash[category] = Category.get_contents(category) }
end
For this solution, content_categories must be a hash, not an array as you describe. Otherwise not sure where you're getting the key.
contents_by_categories = Hash[*content_categories.map{|k, v| [k, Category.getContents(v.id)]}]

Resources