I have this:
source = MyModel.all.some_filter(some_condition).order("condition123 ASC") # 1
res1 = source.page(params[:page]).per(PAGE_SIZE) # 2
I also want to retrieve 2 more items from source that "surround" res1 - its neighbors :
neighbour1 =
if params[:page] > 1
source.page(params[:page] - 1).per(1).order("condition123 DESC")
else
nil
end
neighbour2 =
unless last_page(source)
source.page(params[:page] + 1).per(1)
else
nil
end
and merge the results all together:
full_result = neighbour1 + res1 + neighbour2
First off all, is there any way to make it more effective?
And how do I merge the results if neighbour1 or neighbour2 are nil? If do this:
if params[:page] > 1
source += source.page(params[:page] - 1).per(1).order("condition123 DESC")
end
then source will be converted to Array and I won't be able to use page anymore. I know I could do this via Kaminary.page but I prefer just to get source not to convert itself to array and remain MyModel::ActiveRecord_Relation
Related
Let's say I have a loop repeating 1000 times.
Previous to the loop, I set 1000 variables, like so:
#n1 = "blabla"
#n2 = "blabla"
#n3 = "blabla"
#n4 = "blabla"
...
I also have a variable #count that counts which iteration the loop is on, ie. it starts at 1 and increases by 1 with each loop. What I want to do is print #n1 if #count = 1, print #n2 if #count = 2, and so forth. In other words, I want ruby to use the value of #count to decide which #n_ variable to use. I don't want to use conditional statements, because I'd need 1000 of them.
Something like this:
#count = 1
if #count < 1001
puts #("n + #count")
#count = #count + 1
end
Is there a way to do this?
Let's say you have an instance called foo, with a thousand instance variables, to loop through them all you could do:
foo.instance_variables.each do |v|
p foo.instance_variable_get(v)
end
That said, you can also use the string name to fetch them:
1000.times do |count|
p foo.instance_variable_get("#n#{count}")
end
Yes, you can do do this:
if #count < 1001
instance_variable_set("##{#count}", #count + 1)
end
It would be more idiomatic to store in a hash, e.g.
h = {}
if #count < 1001
h[#count] = #count + 1
end
While you can use something like instance_variable_get, you would usually use an array or an hash to store your strings:
n = ["blabla", "blabla", "blabla", ... ]
count = 0
if count < 1000
puts n[count]
count += 1
end
Let's say i have two relation arrays of a user's daily buy and sell.
how do i iterate through both of them using .each and still let the the longer array run independently once the shorter one is exhaused. Below i want to find the ratio of someone's daily buys and sells. But can't get the ratio because it's always 1 as i'm iterating through the longer array once for each item of the shorter array.
users = User.all
ratios = Hash.new
users.each do |user|
if user.buys.count > 0 && user.sells.count > 0
ratios[user.name] = Hash.new
buy_array = []
sell_array = []
date = ""
daily_buy = user.buys.group_by(&:created_at)
daily_sell = user.sells.group_by(&:created_at)
daily_buy.each do |buy|
daily_sell.each do |sell|
if buy[0].to_date == sell[0].to_date
date = buy[0].to_date
buy_array << buy[1]
sell_array << sell[1]
end
end
end
ratio_hash[user.name][date] = (buy_array.length.round(2)/sell_array.length)
end
end
Thanks!
You could concat both arrays and get rid of duplicated elements by doing:
(a_array + b_array).uniq.each do |num|
# code goes here
end
Uniq method API
daily_buy = user.buys.group_by(&:created_at)
daily_sell = user.sells.group_by(&:created_at
buys_and_sells = daily_buy + daily_sell
totals = buys_and_sells.inject({}) do |hsh, transaction|
hsh['buys'] ||= 0;
hsh['sells'] ||= 0;
hsh['buys'] += 1 if transaction.is_a?(Buy)
hsh['sells'] += 1 if transaction.is_a?(Sell)
hsh
end
hsh['buys']/hsh['sells']
I think the above might do it...rather than collecting each thing in to separate arrays, concat them together, then run through each item in the combined array, increasing the count in the appropriate key of the hash returned by the inject.
In this case you can't loop them with each use for loop
this code will give you a hint
ar = [1,2,3,4,5]
br = [1,2,3]
array_l = (ar.length > br.length) ? ar.length : br.length
for i in 0..array_l
if ar[i] and br[i]
puts ar[i].to_s + " " + br[i].to_s
elsif ar[i]
puts ar[i].to_s
elsif br[i]
puts br[i].to_s
end
end
In my rails app, I want to add few values present in one column in the form of key-value pair. I am not getting the way to add them.
(byebug) p #timing_params.data_date_wise
{"2"=>"7", "3"=>"8", "4"=>"9", "5"=>"10", "6"=>"11", "9"=>"", "10"=>"", "11"=>""
, "12"=>"", "13"=>"", "16"=>"", "17"=>"", "18"=>"", "19"=>"", "20"=>"", "23"=>""
, "24"=>"", "25"=>"", "26"=>"", "27"=>"", "30"=>"", "31"=>""}
Controller:
total_hour = 0
total_day_count = #timing_params.data_date_wise.count
puts "total_day_count = #{total_day_count}"
for i in 1..total_day_count
total_hour+= #timing_params.data_date_wise["i"] if #timing_params.data_date_wise["i"].to_i > 0
puts "date : #{#timing_params.data_date_wise['i']}"
end
puts "TotalHour : #{total_hour}"
Another problem I think is - All details are not in sequence so that only values will be calculated. For example count is 22 then as per the data - 23 to 31 will be missed.
Please suggest something...
How about changing your for loop to iterate through the hash like this:
#timing_params.data_date_wise.each do |date,value|
total_hour += value.to_i if value.to_i > 0
end
I have two models (Folder and Document) which I need to show in a single view together. However, to reduce the number of queries sent I am collecting the Documents only if the folders are less than 12 (my :per_page). While this is working fine, I am stuck in a particular case,
When my total documents are less than 12 and folders are less than 12 but together are more than 12, the pagination fails.
Below is the code to calculate which page to be shown where f_page returns the page for the Folder pagination and d_page returns the page number for the document collection.
def f_page(page_cnt, size)
page_cnt.present? and size.nonzero? ? page_cnt.to_i <= (size/12 + (size%12==0 ? 0 : 1)) ? page_cnt.to_i : (size / 12 ) + (size%12==0 ? 0 : 1) : 1
end
def d_page(page_cnt, fc, dc)
page_cnt = page_cnt.present? ? page_cnt : 1
puts page_cnt
dpg = 1
if (fc/12+1 == page_cnt.to_i)
dpg = 1
elsif ((fc/12+1) < page_cnt.to_i)
if (fc < 12)
unless (dc <= 12)
dpg = page_cnt
else
dpg = 1
end
else
(fc/12 == 0) ? (dpg = page_cnt.to_i - (fc/12+1)) : (dpg = page_cnt.to_i - (fc/12))
end
end
puts "dpg = #{dpg}"
return dpg
end
Both are together collected and paginated which is shown in the view.
f = Folder.action_folder_collection(#action, current_user).paginate(:page => params[:page], :per_page => 12)
if (f.count < 12)
d = Document.action_document_collection(#action, current_user).paginate(:page => d_page(params[:page], total_folders, total_documents), :per_page => per_page-f.count)
end
collection << f
collection << d
#collection = collection.flatten.paginate(:page => 1,:per_page => 12,:total_entries => total)
How do I solve it?
I have just solved the similar problem. My paginate_catalog_children helper receives either an AR collection or an array of collections as a parameter and returns WillPaginate::Collection object containing elements from all collections.
def paginate_catalog_children catalog_children, page
per_page = 20
if catalog_children.is_a? ActiveRecord::Relation
catalog_children.paginate(:per_page => per_page, :page => page)
else
# paginating array of collections
WillPaginate::Collection.create(page, per_page) do |pager|
catalog_children_counts = catalog_children.map(&:count)
result = []
offset = pager.offset
items_left = pager.per_page
catalog_children.each_with_index do |collection, index|
break if items_left == 0
if catalog_children_counts[index] <= offset
# skip this collection
offset -= catalog_children_counts[index]
else
collection_items = collection.limit(items_left).offset(offset)
result += collection_items
items_left -= collection_items.size
offset = 0
end
end
pager.replace(result)
pager.total_entries = catalog_children_counts.sum
result
end
end
end
I want to make a loop on a variable that can be altered inside of the loop.
first_var.sort.each do |first_id, first_value|
second_var.sort.each do |second_id, second_value_value|
difference = first_value - second_value
if difference >= 0
second_var.delete(second_id)
else
second_var[second_id] += first_value
if second_var[second_id] == 0
second_var.delete(second_id)
end
first_var.delete(first_id)
end
end
end
The idea behind this code is that I want to use it for calculating how much money a certain user is going to give some other user. Both of the variables contain hashes. The first_var is containing the users that will get money, and the second_var is containing the users that are going to pay. The loop is supposed to "fill up" a user that should get money, and when a user gets full, or a user is out of money, to just take it out of the loop, and continue filling up the rest of the users.
How do I do this, because this doesn't work?
Okay. What it looks like you have is two hashes, hence the "id, value" split.
If you are looping through arrays and you want to use the index of the array, you would want to use Array.each_index.
If you are looping through an Array of objects, and 'id' and 'value' are attributes, you only need to call some arbitrary block variable, not two.
Lets assume these are two hashes, H1 and H2, of equal length, with common keys. You want to do the following: if H1[key]value is > than H2[key]:value, remove key from H2, else, sum H1:value to H2:value and put the result in H2[key].
H1.each_key do |k|
if H1[k] > H2[k] then
H2.delete(k)
else
H2[k] = H2[k]+H1[k]
end
end
Assume you are looping through two arrays, and you want to sort them by value, and then if the value in A1[x] is greater than the value in A2[x], remove A2[x]. Else, sum A1[x] with A2[x].
b = a2.sort
a1.sort.each_index do |k|
if a1[k] > b[k]
b[k] = nil
else
b[k] = a1[k] + b[k]
end
end
a2 = b.compact
Based on the new info: you have a hash for payees and a hash for payers. Lets call them ees and ers just for convenience. The difficult part of this is that as you modify the ers hash, you might confuse the loop. One way to do this--poorly--is as follows.
e_keys = ees.keys
r_keys = ers.keys
e = 0
r = 0
until e == e_keys.length or r == r_keys.length
ees[e_keys[e]] = ees[e_keys[e]] + ers[r_keys[r]]
x = max_value - ees[e_keys[e]]
ers[r_keys[r]] = x >= 0 ? 0 : x.abs
ees[e_keys[e]] = [ees[e_keys[e]], max_value].min
if ers[r_keys[r]] == 0 then r+= 1 end
if ees[e_keys[e]] == max_value then e+=1 end
end
The reason I say that this is not a great solution is that I think there is a more "ruby" way to do this, but I'm not sure what it is. This does avoid any problems that modifying the hash you are iterating through might cause, however.
Do you mean?
some_value = 5
arrarr = [[],[1,2,5],[5,3],[2,5,7],[5,6,2,5]]
arrarr.each do |a|
a.delete(some_value)
end
arrarr now has the value [[], [1, 2], [3], [2, 7], [6, 2]]
I think you can sort of alter a variable inside such a loop but I would highly recommend against it. I'm guessing it's undefined behaviour.
here is what happened when I tried it
a.each do |x|
p x
a = []
end
prints
1
2
3
4
5
and a is [] at the end
while
a.each do |x|
p x
a = []
end
prints nothing
and a is [] at the end
If you can I'd try using
each/map/filter/select.ect. otherwise make a new array and looping through list a normally.
Or loop over numbers from x to y
1.upto(5).each do |n|
do_stuff_with(arr[n])
end
Assuming:
some_var = [1,2,3,4]
delete_if sounds like a viable candidate for this:
some_var.delete_if { |a| a == 1 }
p some_var
=> [2,3,4]