Rails how to set table in find command - ruby-on-rails

After migrating from Rails 3.2.12 to 4.0.0 this dosn't work any more
#course = #current_client.courses.find params[:id], :conditions => ["active = ? AND bookable = ?", true, true]
The Problem is that it tries to get data from table
clients_course_objects
right table should be
clients_course
Is it possible to fix this?

The Rails 4 way to run this query is:
#current_client.courses.where("active = ? AND bookable = ?", true, true).find(params[:id])

Does this work?
#course = #current_client.courses.where("id = ? AND active = ? AND bookable = ?", params[:id], true, true)

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

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'

In ActiveRecord, how to check if an object property is nil?

Suppose I need to check if a certain property is not set.
I imagine something like this but It doesn't work.
#users = User.find_all_by_role(["role = ?",nil])
I tried some other variants with no luck.
I guess this should be pretty straightforward.
Thank you!
Using ActiveRecord's pre-3.0 syntax:
#users = User.find(:all, :conditions => { :role => nil })
After 3.0 you can write:
#users = User.where(:role => nil)
Try: #users = User.find(:all, :conditions=>'role is null')
#users = User.where(:roles => nil).all
There's no need to break into SQL for this.

Rails 3 ActiveRecord Questions

I have a few queries for you Rails 3 gurus out there. How can you accomplish the following?
The following pseudocode is currently invalid. Thanks all.
#items = (#itemsA + #itemsB).order("name ASC")
#item = Item.where("type = ?" and "condition = ?", "book", "new")
#commenteditems = Item.find_all_by_type_and_condition("book", "new").include("notes").select("item[name]", "notes[note]")
#selecteditems = #items.where("select = ?", true)
I believe what you're looking for is scopes.

How to have condition in ActiveRecord update

I am trying to update a record. Am using the following code.
Proddiscount.update({:prodid => params[:id]}, {:discount=>params[:discount]})
Query Im trying to have is:
update proddiscount set discount = '' where prodid = ''
If prodif is not a primary key, use update_all (read more here)
Proddiscount.update_all("discount = #{params[:discount]}", ["prodid = ?", params[:prodid])
But it won't trigger validations.
How about doing this:
#prod = Proddiscount.find(:first, :conditions => {prodid => params[:id]})
#prod.update_attributes({:discount=>params[:discount]})

Resources