How to use searchkick search method on an array - ruby-on-rails

I have a Project and a Meeting Model in my application.
Query in meetings controller is as follows:
all_meetings = Project.get_all_meetings #project
meetings = Kaminari.paginate_array(all_meetings).page(params[:page]).per params[:per_page]
render_collection meetings, Meeting
In project model:
def self.get_all_meetings project
active_meetings = project.meetings.without_deleted.order updated_at: :desc
cancelled_meetings = project.meetings.only_deleted.order updated_at: :desc
active_meetings + cancelled_meetings
end
I want to use search method on all_meetings variable like all_meetings.search but getting error as
NoMethodError (undefined method `search' for #<Array:0x00007f20b0b74238>
Did you mean? bsearch
each):
app/controllers/api/v1/pm/meetings_controller.rb:9:in `index'
app/controllers/api/v1/base_controller.rb:85:in `set_global'
app/controllers/api/v1/base_controller.rb:117:in `handle_exceptions'
How can I do this?

you have to convert this whole logic to elasticsearch functionalities. put pagination and "get_all_meetings" filtering mechanism to a searchkick where clause. you can add more data to elasticsearch indexes by overriding "serach_data" function.

Related

undefined method `>' for nil:NilClass Ruby on Rails (NoMethodError)

So for some odd reason I keep getting this error:
undefined method `>' for nil:NilClass
Rails.root: C:/Sites/booking_saloniser - Calendar
Application Trace | Framework Trace | Full Trace
app/models/user.rb:11:in `block in upcoming_appointments'
app/models/user.rb:11:in `upcoming_appointments'
app/controllers/appointments_controller.rb:8:in `index'
appointment controller
def index
#upcoming_appointments = current_user.upcoming_appointments
end
user model
def upcoming_appointments
appointments.order(appointment_time: :desc).select { |a| a.appointment_time > (DateTime.now) }
end
Any chance someone could help me get around this error?
It appears one or more of your appointment records has a nil appointment_time.
If this is not expected, you should add a validation on the model that it's not null, and fix your existing data.
Otherwise (and this works as quick fix), you can chain a query to not include records with a nil appointment time:
def upcoming_appointments
appointments
.order(appointment_time: :desc)
.where.not(appointment_time: nil)
.select { |a| a.appointment_time > (DateTime.now) }
end
If where.not isn't available (if you're using an older version of rails), you can also use where("appointment_time != null")

Facing issue NoMethodError (undefined method `include' for "56":String):

I am facing issue while writing this code
def sim_state
sim_employees = SimEmployee.find(params[:id].include(:employees))
respond_to do |format|
format.js {
render :layout => false,
:locals => {
:sim_employees => sim_employee
}
}
end
end
and in my sim_states.js.erb
$('#simState').text('<%= sim_employee.employees.app_state%>');
So it gives me this error
NoMethodError (undefined method `include' for "56":String):
when i use it with includes then it gives
undefined method `includes'
Please guide me how to solve this.
The reason is simple
params[:id]
is return you a String value which is "56" and includes works with ActiveRecord::Relation and not string. So you are not able to fire the query that ways.
SimEmployee.find(params[:id])
This will return again a result and not relation. Try using
SimEmployee.where(id: params[:id]).includes(:employees).first
This should help you.
PS : Using includes for one record is same as firing two independent queries i.e.
#sim_employee = SimEmployee.find(params[:id])
#emplyess = #sim_employee.employees
There is a simple solution for this:
SimEmployee.includes(:employees).find params[:id]
SimEmployee.find(params[:id].include(:employees))
This line is causing the issue, you are calling include(:employees) on params[:id] which would be a number.
And you can't actually do .find(params[:id].include(:employees) because find method returns an instance of Model class, in this case, an instance of SimEmployee class, and you can't run method include on it.
Edit:
SimEmployee.includes(:employees).where()
You can pass a hash in where(). This works if your SimEmployee model has has_many relation to Employee model.

Undefined method 'id' for Class, but the console loads fine

I can access the shopping_list_products loaded in this API call from the console with no error.
I get a 500 on the API, however:
ActionView::Template::Error (undefined method `id' for #<Class:0x007f6cca7e1dd0>):
2015-01-26T23:51:07.608697+00:00 app[web.1]: 1: collection :#shopping_list_products
2015-01-26T23:51:07.608699+00:00 app[web.1]: 2: extends 'api/v1/shopping_list_products/_show'
index.json.rabl:
collection :#shopping_list_products
extends 'api/v1/shopping_list_products/_show'
show.json.rabl:
object :#shopping_list_product
extends 'api/v1/shopping_list_products/_show'
_show.json.rabl:
object :shopping_list_product
attribute(:id, :if => lambda { |m| m.id })
attributes :shopping_list_retailer_id, :product_id, ...
... more attributes
child :product, partial: 'api/v1/products/_show'
child :product_category, partial: 'api/v1/product_categories/_show'
node(:url) do |shopping_list_product|
api_v1_schedule_requisition_plan_shopping_list_shopping_list_retailer_shopping_list_product_path(#schedule, #shopping_list_retailer, shopping_list_product, format: :json)
end
EDIT: I removed the id attribute and then ran into the next error, "undefined method shopping_list_retailer_id for Class:". Why is this happening?
EDIT: Found out it's my code called from the controller.. if I return
#shopping_list_retailer.shopping_list_products
it works fine.
But I do this instead:
api :GET, '/schedule/:schedule_id/requisition_plan/shopping_list/shopping_list_retailers/:shopping_list_retailer_id/shopping_list_products', 'List all shopping list products for Shopping List for Requisition Plans for Schedule in the database'
param :query, String, desc: "Scoped_search style query string"
def index
#shopping_list_products = ShoppingListProductsIndexQuery.new(#shopping_list_retailer, params[:query]).shopping_list_products
end
class ShoppingListProductsIndexQuery
attr_reader :shopping_list_retailer, :query
def initialize(shopping_list_retailer, query)
#shopping_list_retailer = shopping_list_retailer
#query = query
end
def shopping_list_products
#shopping_list_retailer.shopping_list_products.ordered_by_product_category_type_and_product_category_name_and_product_name.search_for(query)
end
end
Still confused why undefined method id for Class is hit in the rabl view.
Your error message (undefined method 'id' for #<Class:0x007f6cca7e1dd0>) is saying the undefined method is on an instance of Class instead of an instance of ShoppingListProduct (or whatever your actual class name is).
This means the wrong thing is being rendered.
Probably because you are using:
object :#shopping_list_products
# and
object :#shopping_list_product
Instead of:
object #shopping_list_products
# and
object #shopping_list_product
Remove the :s.
Also, in your _show.json.rabl file, you really don't need
object :shopping_list_product
as this is ignored when the RABL template is being used as a partial (https://github.com/nesquena/rabl/wiki/Reusing-templates)

Trying to .dup an activerecord entry, getting "undefined method `stringify_keys' "

I am using Deep clonable gem to clone an entry with some associations. Here is my action
def copy
#group = Group.find(params[:id])
#newgroup = Group.new(#group.dup :include => :lessons).save
redirect_to #newgroup
end
And that's what I get when trying to do it:
undefined method `stringify_keys' for #<Group name: "40-201B",
created_at: nil, updated_at: nil>
How can I fix it or do what I need the other way around?
You probably don't need to combine new and dup the way you are... try
#newgroup = #group.dup(:include => :lessons).save
You should also consider some logic to confirm the item is valid and was actually saved before you redirect.

Search in child model in Ruby on Rails

A company model has many tag and has a country_id field. I would like to find:
all companies, located in a certain county
all companies, located in a certain county and has a certain tag if params[:tag] is present.
The first query is pretty easy
Company.where(:country_id => params[:country_id])
As for second one, I tried some queries and nothing worked
companies = Company.where(:country_id => params[:country_id])
companies = Company.tags.where(:name=> params[:tag])
undefined method `tags' for #<Class:0x000000055dfb60>
If I put
Company.tags.where(:name=> params[:tag])
then the error is the same
undefined method `tags' for #<Class:0x000000055dfb60>
In Rails console a command Company.first.tags receives all tags as it does.
UPDATE: this works
Company.joins(:tags).where("tags.name = ?", query_hash[:tag])
But I don't understand yet how to do something like this
my_conditions = get_search_conditions
if query_hash[:tag].present?
companies = Company.all(:conditions => my_conditions).joins(:tags).where("tags.name = ?", query_hash[:tag])
else
companies = Company.all(:conditions => conditions)
end
The error is
undefined method `all' for #<Array:0x007fbec8063e00>
The error is undefined method all for #<Array:0x007fbec8063e00>
It should work if you replace Company.all with Company.where
check if this works - Company.joins(:tags).where("tags.name",params[:tag])

Resources