Rails 3 ActiveRecord Questions - ruby-on-rails

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.

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'

Rails how to set table in find command

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)

rails 4 how to use where and where in condition simultaneously

I have the following query
model = (1,2,3,4)
#posts = Post.where(category_id: id, product_model_id: model)
My above query is justing taking the 1 from model how can i use where in condition over here
Edit-1
This piece of code works but I don't feel this as a good code right?
#posts = Post.where("category_id = ? and product_model_id in (#{model})", id)
Edit-2
If I use
#posts = Post.where("category_id = ? and product_model_id in (?)", id, model)
Throwing error as
invalid input syntax for integer: "15,16" because my input is like this
select * from posts where category_id=5 and product_model_id in ('15,16')
How to correct it then..
model_ids = model.split(",").map(&:to_i)
#posts = Post.where(category_id: id, product_model_id: model_ids)
or
model_ids = model.split(",").map(&:to_i)
#posts = Post.where("category_id = ? AND product_model_id IN (?)", id, model_ids)
According to the rails guide, you can pass in an array to where and it should understand that you want to use IN. See the guide here: http://guides.rubyonrails.org/active_record_querying.html#subset-conditions
I'm a little confused by your syntax, since model = (1, 2, 3, 4) doesn't look like valid array syntax.
Relevant part of the guide:
Client.where(orders_count: [1,3,5])
You could use arel, but I'd just do something like:
#posts = Post.where("category_id = ? AND product_model_id IN (?)", id, model)

searchlogic - array error

I have the following controller method
def app_used_by_Lab
per_id = params[:id]
#search1 = Apparatus.used_by_specific_lab(per_id).search(params[:search]) # both 'used_by_specific_lab' & 'lab_created' are named_scopes which return results from the same table
#search2 = Apparatus.lab_created(per_id).search(params[:search])
#search = #search2 + #search1
#search.order ||= :descend_by_RPR_DATE_CREATED
#apparatuses = #search.paginate(:page => params[:page], :per_page => 10)
end
If I change the code to '#search = #search1', it works fine and return me the results but when I do '#search = #search2 + #search1', I get the error message below:
TypeError in ApparatusesController#app_used_by_Lab
can't convert Searchlogic::Search into Array
Is it not possible to use searchlogic on arrays?
Is there any solution to the above problem?
Thanks a lot for your precious help.
Please Try this:
#search = #search2.to_s + #search1.to_s
try this:
#search = #search2.concat(#search1)

Resources