I have this array that looks like this.
#a = ["foo","bar"].join(",")
But i would like to retrieve the "foo" and "bar" through a loop from my database instead of creating them manually and insert them into the array. How would i do that? For instance i have data, in which i want all the usernames to be put in an array and be seperated by the ",". How can i put all usernames into the array?
#data = Data.all
#data.each do |d|
d.username
end
usernames = Data.all.map(&:username)
joined = usernames.join(',')
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 need to Separate multiple Array entries with a newline.
the names in the aray value. Again I want to split the array like seperate names.
Eg. names="alice\nbob"
and I want names=alice&names=bob. How can I get that??
Try this:
names="alice\nbob"
names = names.split("\n").map{|item| "names=#{item}"}.join("&")
#=> "names=alice&names=bob"
If intention is to have it in form of query parameters, I will suggest using Addressable::URI:
require "addressable/uri"
def return_query(str)
uri = Addressable::URI.new
uri.query_values = {:names => str.split("\n") }
uri.query
end
return_query("alice\nbob") #=> "names=alice&names=bob"
return_query("alice in wonderland\nbob") #=> "names=alice%20in%20wonderland&names=bob"
return_query("alice&wonderland\nbob") #=> "names=alice%26wonderland&names=bob"
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
So my Question is, that there is an Object.weight.
I want to find all Objects where user_id is #user.id, then put all the Object.weight
attributes into an array, only where attribute is not nil.
like
#o = Object.find.where(:user_id => #user.id)
#a << #o.weight.where(:weight true)
And then i would like to list all those from the array, with a ',' separator.
all the best!
Object.where(:user_id => #user.id).collect(&:weight).compact
collect fetches all the elements from an Arrays Items and compact removes all nil objects from the array.
For the view:
arr.join(", ")
try
#weights = Record.where(user_id: #user.id).where('weight IS NOT NULL').map(&:weight)
puts #weights.join(',')
I've got around 15 parameters being submitted (using tag helpers as no data related to a model). Is there a more efficient way of accessing and maybe storing the params into a hash/array as apposed to having to push all the params into a new array using something like the below:
array = []
array << param["a"]
array << param["b"]
array << param["c"]
array << param["d"]
etc..
If all you want are the values then you can do:
array = hash.values
or more specifically to your question:
param_array = param.values
values returns an array of all the values in the hash.