searchlogic - array error - ruby-on-rails

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)

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

Getting a ActiveModel::ForbiddenAttributesError in controller

I'm implementing a search method in a controller, and I keep getting this error:
ActiveModel::ForbiddenAttributesError in TripsController#search
Extracted source (around line #22):
The offending line is within the following block:
if params[:search].present?
#trips = Trip.where("destination LIKE :destination", {:destination=> "%#{params[:search].first}%"}).where(#filters)
else
#trips = (Trip.where(#filters)) rescue (Trip.where(#filters).paginate(:page => params[:page], :per_page => 10))
end
Below is the full method code:
def search
#languages = Trip.pluck(:language).uniq
#interests = Trip.pluck(:interests).uniq
#destinations = Trip.pluck(:destination).uniq
#filters = params.slice(:language,:interests)
#search_param = params[:search].first rescue nil
#language_param = params[:language]
#intersts_param = params[:interests]
if params[:search].present?
#trips = Trip.where("destination LIKE :destination", {:destination=> "%#{params[:search].first}%"}).where(#filters)
else
#trips = (Trip.where(#filters)) rescue (Trip.where(#filters).paginate(:page => params[:page], :per_page => 10))
end
end
You need to use the Rails strong parameters.
Even if you are not using the parameters to to create a model Rails will raise an exception if you use params.slice and pass the result to a query.
This is due of the pretty pro-active approach that came with Rails 4 to avoid mass-assignment vulnerabilities.
def search
#languages = Trip.pluck(:language).uniq
#interests = Trip.pluck(:interests).uniq
#destinations = Trip.pluck(:destination).uniq
#filters = params.permit(:language,:interests)
#search_param = params.permit(search: [])
#language_param = #filters[:language]
#intersts_param = #filters[:interests]
if params[:search].present?
# you don't need named placeholders for a single parameter...
#trips = Trip.where("destination LIKE ?", params[:search].first)
.where(#filters)
else
#trips = (Trip.where(#filters)) rescue (Trip.where(#filters).paginate(:page => params[:page], :per_page => 10))
end
end
However in .where(#filters) you are breaking a cardinal rule and passing user input straight into a SQL query.
Instead you should use something like this:
Trip.where(language: #filters[:language])
This will create a "WHERE trips.language = ?" query with a placeholder which removes the SQL injection vulnerability.

Rails Activerecord Ambiguous column name on .joins

Here is my code. It work fine if I have something in the :search field or if I have something in the :supplier field but if I have something in both i get "Ambiguous column name 'NUMBER'". Is there a way to select AS or something?
#date_from = params[:date_from] || Date.today.beginning_of_month.strftime('%m/%d/%Y')
#date_to = params[:date_to] || Date.today.strftime('%m/%d/%Y')
q = "%#{params[:search]}%"
#products = Product.where("DISCONT = ? AND NONPRODUCT = ?" ,0,0)
#products = #products.where('NUMBER like ?' ,q) if params[:search].present?
#products = #products.joins(:supplier_items).where('SUPPLIER = ?' ,params[:supplier]) if params[:supplier].present?
#products = #products.paginate(page: params[:page], per_page: 25)
Just prefix the number with the table name
For example:
#products = Product.where(:discount => 0, :nonproduct => 0)
#products = #products.where('products.number like ?', query)
I'm guessing your suppliers table and products table both have a column named "number". Honestly, you'd be best off running a migration to change the column names now (maybe supplier_num / product_num) because keeping it something as generic as "number" will likely keep causing you headaches.

Ruby loop problems

I am working on a script that is supposed to be writing a list of items to a hash, but for some reason its only placing the last item in the loop in the hash... I have been working on this script all day, so I am pretty sure its something I am just missing.
Here is the script
#mr = MediaRating.where("user_id = ?", session['user_credentials_id'])
#mr.each do |rating|
#m = Media.where("id = ?", rating.media_id)
#m.each do |m|
s = Profile.find_by_subscriber_id(m.subscriber_id)
#h_lang = Language.find_by_code(s.language)
#history = {m.title => #h_lang.english}
end
end
There are multiple records in the MediaRating table so I know it has to do something with how my loop is. Thanks in advance for the help!
Working code:
#mr = MediaRating.where("user_id = ?", session['user_credentials_id'])
#mr.each do |rating|
#m = Media.find(rating.media_id)
s = Profile.find_by_subscriber_id(#m.subscriber_id)
#h_lang = Language.find_by_code(s.language)
#history[#m.title] = #h_lang.english
end
In the last line, you are over-writing the entire #history hash instead of adding a new key/value pair to it. I'm guessing that isn't what you intended. Change this line:
#history = {m.title => #h_lang.english}
to this:
#history[m.title] = #h_lang.english

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.

Resources