Ruby: compare hash to hash value trouble - ruby-on-rails

I have such part of "ghost look like" code (but it so must be, as db is huge and have many tables):
def search_group
#search_trees = SearchTree.all
#designation = Designation.find(:all, :conditions => { :DES_ID => #search_trees.map(&:STR_DES_ID)})
#text = DesText.find(:all, :conditions => { :TEX_ID => #designation.map(&:DES_TEX_ID)})
#search_result = #text.find_all{|item| item.TEX_TEXT.include?(params[:search_group_text])}
#designation_back = #designation.find_all{|item| item.DES_TEX_ID == #search_result.TEX_ID}
#search_trees_back = #search_trees.find_all{|item| item.STR_DES_ID == #designation_back.DES_ID}
respond_to do |format|
format.html
end
end
I try to compare
#designation_back = #designation.find_all{|item| item.DES_TEX_ID == #search_result.TEX_ID}
but i get errors, something bad...undefined method `TEX_ID'. As i think, it's via i compare hash and hash in bad way... How can i do this?

#search_result = #text.find_all{|item| item.TEX_TEXT.include?(params[:search_group_text])}
#designation_back = #designation.find_all{|item| item.DES_TEX_ID == #search_result.TEX_ID}
it's because #search_result is an array and not an object where you can call that method on.

#search_results is an array. If you know it is returning just one result, you can do #search_results[0].Tex_Id, otherwise have to loop through for each value of #search_results.
try 'pry' gem to debug what results you are getting from each assignment.

Related

how append an object to association relation in rails?

In a rails 4.1 application I need to add an object to an "AssociationRelation"
def index
employee = Employee.where(id_person: params[:id_person]).take
receipts_t = employee.receipts.where(:consent => true) #gives 3 results
receipts_n = employee.receipts.where(:consent => nil).limit(1) #gives 1 result
#I would need to add the null consent query result to the true consent results
#something similar to this and the result is still an association relation
#receipts = receipts_t + receipts_n
end
Is there a simple way to do this?
A way of solving this:
def index
employee_receipts = Employee.find_by(id_person: params[:id_person]).receipts
receipts_t = employee_receipts.where(consent: true)
receipts_n = employee_receipts.where(consent: nil).limit(1)
#receipts = Receipt.where(id: receipts_t.ids + receipts_n.ids)
end
Unfortunately .or() can't be used here because it's only available from Rails v5.0.0.1
you could do this way
receipts_t_ids = employee.receipts.where(:consent => true).pluck(:id)
receipts_n_ids = employee.receipts.where(:consent => nil).limit(1).pluck(:id)
#receipts = Receipt.where(id: receipts_t_ids + receipts_n_ids)
To avoid extra queries and keeping arrays in memory, you can use or
Like this:
def index
employee_receipts = Employee.find_by(id_person: params[:id_person]).receipts
#receipts =
employee_receipts.where(consent: true).or(
employee_receipts.where(consent: nil).limit(1)
)
end

Update database using a loop on Ruby On Rails

I'm trying to do a for each loop to update my database. I was using this piece of code to make the update:
def update
respond_to do |format|
#t_id = params[:t_id]
#t_order = params[:order]
#t_relation = TRelation.where('t_id' => #t_id)
#i = 0;
#t_order.each do |p|
#t_relation = TRelation.where('t_id = ? and
video_id = ?', #t_id, p[1][#i])
#i = #i + 1
#t_relation[0].t_order = #i
#t_relation[0].save
end
format.json { render :nothing => true, :status => 200, :content_type => 'text/html' }
end
end
It does not loop through; it goes through it one time and stops. I don't understand what's happening.
This is the content of params[:order]
params[:order] = {ActionController::Parameters} ActionController::Parameters (1 element)
'0' = Array (3 elements)
[0] = "7"
[1] = "5"
[2] = "3"
And if I make a #timeline_order.inspect I get this:
{"0"=>["7", "5", "3"]}
How can I loop through it? I have no idea
It iterates once because params[:order] only has one element. What you want to do is iterate on #t_order["0"], which has 3 elements.
Also, you should avoid all that logic within the respond_to. You can (should) define the variables outside of it.

A better way to do conditional ActiveRecord statements?

I'm trying to figure out a better way to have one query here. I want to be able to send something to last where statement a wildcard so I can select all vendors. Right now if i don't include that line it doesn't filter by vendor so I essentially get all the purchase requests.
Any thoughts of a cleaner way to do these sorts of queries?
if #vendor == "0" #checks for vendor
#purchase_requests = PurchaseRequest.includes(:purchase_order)
.where(:created_at => #date_start..#date_end)
.where(:total_cost => #cost_beginning..#cost_end)
else
#purchase_requests = PurchaseRequest.includes(:purchase_order)
.where(:created_at => #date_start..#date_end)
.where(:total_cost => #cost_beginning..#cost_end)
.where("purchaseorder.VendorRef_ListID = ?", #vendor)
end
there must be some better solution, but try this
#purchase_requests = PurchaseRequest.includes(:purchase_order).where(created_at: #date_start..#date_end, total_cost: #cost_beginning..#cost_end)
#purchase_requests = #purchase_requests.where("purchaseorder.VendorRef_ListID = ?", #vendor) unless #vendor == "0"
Here is a simplified version:
#purchase_requests = PurchaseRequest
.includes(:purchase_order)
.where(created_at: #date_start..#date_end)
.where(total_cost: #cost_beginning..#cost_end)
#purchase_requests = #purchase_requests.where('purchase_orders.VendorRef_ListID = ?', #vendor) unless #vendor == '0'

Slice/Map Ordered Hash

I am writing a "Punch Clock" application for my office.. I am working on the controller for the "TIme Card" view which should list a users punches for a given week, and total DAILY then add the TOTAL for the week. I have figured out how to get the time diff between all of the punches with slice/map, my issue is that when I try to do this on the ordered hash (grouped by days) I get undefined method `created_at' for #, I know I must be missing some syntax somewhere, your help is greatly appreciated...
Here is my controller...
Note that if i call #in_out_lenghts on #punches, this works and gives me the total for the week, but #punches_days gives me an error, therefore I can not keep a running tally....
def mytimecard
#week = params[:week].to_s
if #week == "lastweek"
#punches = Punch.lastweek.where("user_id = ?", params[:user])
else
#punches = Punch.thisweek.where("user_id = ?", params[:user])
end
#punches_days = #punches.group_by { |t| t.created_at.beginning_of_day}
if #punches.count%2 == 0
#in_out_lengths = #punches_days.each_slice(2).map { |a|(a[1].created_at).round(15.minutes) - (a[0].created_at).round(15.minutes) }
#total = ((#in_out_lengths.inject(:+))/60/60)
else
#total = "Can Not Calculate, Odd Number of Punches"
end
respond_to do |format|
format.html # timecard.html.erb
format.json { render :json => #punches }
end
end
group_by will return a hash of days and punches.
{ :day_1 => [ :punch1, :punch2], :day_2 => [ :punch3, :punch4, :punch5 ] }
doing an each_slice and a map will result in some sort of array, but probably not what you meant.
You may have meant to count the number of punches and call something like this
Punch.lastweek.where("user_id = ?", params[:user]).group('date(created_at)')
This would have resulted in the date => punches_count format, at least with mysql.

Rails: How to manipulate find(params[])

I am trying to use Object.find(params[]) to only return objects with :stage_id = integer
Here is my controller code
def show
#lesson = Lesson.find(params[:id])
#stage1 = #lesson.units(params[:stage_id] == 1)
#stage2 = #lesson.units(params[:stage_id] == 2)
Each lesson has many units, each unit has either a stage_id = 1 or stage_id = 2, I want #stage1 to become an array with units that only have a stage_id value of 1. The same goes for stage2.
How can I properly use params to return only units that have the indicated table values?
def show
#lesson = Lesson.find(params[:id])
#stage1 = #lesson.units.first(:conditions => { :stage_id => 1 })
#stage2 = #lesson.units.first(:conditions => { :stage_id => 2 })
end
Ref find
#stage1 = Unit.find(:all, :conditions => ["stage_id=? AND lession_id=?" 1, params[:id]])
#stage2 = Unit.find(:all, :conditions => ["stage_id=? AND lession_id=?" 2, params[:id]])
If Units are "always' going to be structured with Stages, one thing you could do to DRY up your code is to have a Stage model. That allows flexibility to add more stages in the future without breaking code. Assuming that relationship is properly established and data is good, you could do something like the following.
controller code
#lesson = Lesson.find(params[:id])
view code (haml)
- for stage in #lesson.stages
= stage.name
- for unit in stage.units
= unit.name

Resources