I have the following controller action that builds two arrays.
current_event = Event.find(params[:event_id])
campaign_titles = Relationship::CampaignTitleRelationship.where(campaign_id: current_event.campaign_id)
campaign_title_ids = Array.new
campaign_titles.each do |title|
campaign_title_ids << [title.title_id]
end
event_title_ids = Array.new
params[:title_ids].each do |title|
event_title_ids << [title]
end
The two arrays output like this
[["6556"], ["9359"], ["11319"], ["12952"], ["14389"], ["14955"], ["16823"]]
[[6556], [9359], [11319], [12952], [14389], [14955], [16823]]
I'm trying to compare these two arrays using the - symbol, but am only getting an output of each id, instead of what I expect (nothing) since both arrays contain the same items.
I can see that the first array has quotations around each key inside the bracket. The second does not. How do I compare these two arrays?
Just add params as integers to your array
params[:title_ids].each do |title|
event_title_ids << [title.to_i]
end
Related
I want to get values from a JSON array which I can assign to a table column, however one of the values I need is in an array within an array, within an array. For example, an array of employees and for each employee there is an array of departments which contains an array of floors.
{"employees": [{"name": "bob", "age": 20, "department": ["location": "head office", "floors":["ground", "basement"]], "grade": "supervisor"}]}
The name and age display as expected but I am unsure how to get the floors. I have tried several ways but I am unable to get the "floors". This is my latest attempt but it says invalid conversion of string to integer on the #employee[:department] line. Can someone advise the best way to get this value? Thanks
#employees.each do |i|
employee[:column1] = i[:name]
employee[:column2] = i[:age]
#employee[:department].each do |d|
employee[:column3] = d[:floors]
end
end
The problem is that you're iterating over #employee[:department] when I think you want to be iterating over i[:department].
You could do
#employees.values.flatten.each do |i|
employee[:column1] = i[:name]
employee[:column2] = i[:age]
i[:department].each do |d|
employee[:column3] = d[:floors]
end
end
You could give this a try:
#employees.each do |e|
employee[:column1] = e[:name]
employee[:column2] = e[:age]
employee[:column3] = e[:department].last[:floors]
end
There's no need to iterate since the employee[:column3] = d[:floors] will always end up with the last element of the array. So, you can just go ahead and use last.
Saves you a couple of lines and some typing.
I have two arrays containing strings. I'm trying to iterate through both arrays with nested .each do loops to see if any elements in the first array have a substring of any of the elements in the second array. I'm using .include? within the nested loops to check this. I want the result to be the string printed the number of times it matches an element in partials.
This is the method that isn't working
def orphanCheck(partials, partials1, duplicatesArray)
partials1.each do |i|
partials.each do |j|
if i.include?(j)
duplicatesArray.push(i)
end
end
end
end
I'm using this as a helper method to define partials and partials1
def manipulate(monthEmails, todayEmails, partials, partials1)
monthEmails.each do |i|
email = EmailAddress.new(i.to_s)
partials.push(email.host_name.to_s)
end
todayEmails.each do |j|
todaySignup = j.to_s.slice(11, 100)
partials1.push(todaySignup)
end
end
And then I'm calling the two with the following
manipulate(allUnique, todayEmails, partials, partials1)
orphanCheck(partials, partials1, duplicatesArray)
#puts duplicatesArray
duplicatesArray is printing some strings that shouldn't be matches and it's printing some strings more times than I want. For example, gmail.com isn't in partials at all but me#gmail.com, which is in partials1 once, is being pushed to duplicatesArray three times. If yahoo.com is in partials three times, then I would want me#yahoo.com (from partials1) to be pushed to duplicatesArray three times, for example.
To be sure you could be doing:
partials1.each do |i|
i_ups=i.split('#')[-1]
partials.each do |j|
if i_ups===j
duplicatesArray.push(i)
break
end
end
end
If I understood correctly (partials is only the host part of the email provider and partials1 is the full email address)
A better solution that should give you a correct duplicatesArray would be:
partials1.each do |email_address|
email_host = email_address.split("#").last
duplicatesArray.push(email_address) if partials.include?(email_host)
end
So in my rails app I have a column of text boxes in pairs. There are 4 pairs of textboxes.
Just one of these pairs of text box needs to be filled out (with both textboxes in that pair filled out) for it to be valid.
So I'm trying to loop through them all and add them to a multi dimensional array and to check that at least one row(?) in the array has both values.
def no__values?
all_values = Array.new
txt_a_values = Array.new
txt_b_values = Array.new
self.item.line_items.each do |item|
txt_a_values << item.single.nil? # this is a value from the text box a
txt_b_values << item.aggregate.nil? # this is a value from the text box b
end
all_values << txt_a_values #create multi dimensional array
all_values << txt_b_values
all_values.each do |v|
??? # there needs to be one pair in the array which has both values
end
end
So it should create an array like this
[true][true] # both textboxes are nil
[false][false] # both textboxes have values
[true][true] # both textboxes are nil
[true][true] # both textboxes are nil
the above scenario is valid since there is one pair which BOTH have values
I really don't like the way I'm progressing with this, so I',m looking for some help.
Couldn't you do something like this:
def no__values?
self.item.line_items.each do |item|
return false if !item.single.nil? && !item.aggregate.nil?
end
true
end
That will return false when both values of a pair isn't null, and returns true if each pair had at least one null value.
Edit:
Based on the method name I didn't create the array and instead returns false/true directly.
I think you are looking for the following snippets to solve your problems. I have done it with little simplification. Please let me know if you are looking for this.
def no__values?
all_values = []
self.item.line_items.each do |item|
all_values << [item.single.nil?, item.aggregate.nil?]
end
all_values.each do |v|
puts v # [true, false] or [true, true], [false, false]....
end
end
i was writing code to append new email to an empty array in my Rails App. like this:
#users_email = Array.new
#users_email << User.find_by_id(#comment.user_id).email
#users_email << User.find_by_id(Comment.find_by_id(#parent_id).user_id).email if !#parent_id.nil?
Note: Here #comment is just a hash of id, user_id, parent_id and #parent_id is a the id of a Parent of any Comment. as in Hierarchy of Parent-child.
But instead of having two items in array ex: ["ak#xyz.com", "aks#xyz.com"] I am getting only one item ex:["aks#xyz.com"] after appending second item.
My confusion starts when I try to save above expression in an Instance variable and tries to append the same into an empty array, It behaves as expected.
Ex:
#users_email = Array.new
#first = User.find_by_id(#comment.user_id).email
#second = User.find_by_id(Comment.find_by_id(#parent_id).user_id).email if !#parent_id.nil?
#users_email << #first # ["ak#xyz.com"]
#users_email << #second # ["ak#xyz.com", "aks#xyz.com"]
where as this wasn't the case in previous one.
Can someone please explain whats happening here.
Update
By mistake I have place "=" instead of "<<" while appending first element to the array for the first case and after appending second one I got the result something like "ak#xyz.comaks#xyz.com" but for some reason in my Rails Application I was getting only the first email id in return.ex: ["ak#xyz.com"]
So, probably this question is now not relevant to the issue raised. Sorry for bugging all of you.
That's because you have this check !#parent_id.nil? at the end of the following statement:
#users_email << User.find_by_id(Comment.find_by_id(#parent_id).user_id).email if !#parent_id.nil?
In the first case, #parent_id apparently is nil, so the statement never executes: hence only one value is appended to the array.
Aside: prefer using the array literal syntax (ary = [] over ary = Array.new)
I wish I described this better, but it's the best I know how. I have two classes Cars and Colors. Each can have many of each other through a association class CarColors. The association is set up correctly I'm positive of this but I can't seem to get this to work:
#carlist = Cars.includes(:Colors).all
#carlist.colors
ERROR
#carlist[0].colors
WORKS
My question is how can I iterate over the #carlist without declaring a index as in the successful example? Below is a few things I have tried which also fail:
#carlist.each do |c|
c.colors
end
#carlist.each_with_index do |c,i|
c[i].colors
end
Your first example fails because Car.includes(:colors).all returns an array of cars, not a single car, so the following will fail, because #colors is not defined for the array
#cars = Car.includes(:colors).all
#cars.colors #=> NoMethodError, color is not defined for Array
The following will work, because the iterator will have an instance of car
#cars.each do |car|
puts car.colors # => Will print an array of color objects
end
each_with_index will work as well, but it is a bit different, as the first object
is the same as the each loop car object, the second object is the index
#cars.each_with_index do |car, index|
puts car.colors # => Will print an array of color objects
puts #cars[index].colors # => Will print an array of color objects
puts car == #cars[index] # => will print true
end