Kaminari paginating arrays - ruby-on-rails

In rails 4.0.0.
Why does this work
#employees = Employee.where(:club_id => session[:club_id]).page(params[:page])
but not this?
#payments = Payment.where(:club_id => session[:club_id],
:trading_date => trading_date).page(params[:page])
On the second form I get an array error. I know how to fix it but I am perplexed as to why this occurs?
my error is this
NoMethodError (undefined method `page' for #<Array:0x007ff72845b380>):
app/controllers/payments_controller.rb:30:in `index'

Are you sure you dont want to do
#payments = Payment.where(:club_id => session[:club_id])
.where(:trading_date => trading_date)
.page(params[:page])

Related

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.

Rspec: undefined method `all'

I have to test paginate method in my controller.
In my controller
#categories = Category.paginate(:page => params[:page], :per_page => params[:per_page]).all(:order => 'id ASC')
In my spec
Category.should_receive(:paginate)
get :user_category, { :per_page => 1, :page => 1 }
In my log showing
NoMethodError:
undefined method `all' for nil:NilClass
How do I make this test to pass?
On its own, should_receive will stub the receiver and cause the method to return nil.
You can specify a return value, however:
Category.should_receive(:paginate).and_return(categories_mock)
In the later versions of RSpec, you can also set it up to still call and use the return value of the original method:
Category.should_receive(:paginate).and_call_original
Update:
By the way, the all() call with an argument is no longer supported. You can write the code this way:
#categories = Category.paginate(:page => params[:page], :per_page => params[:per_page]).order('id ASC')
I personally prefer having pagination chained last because it is presentation-related.
For the stubbing, you can use stub_chain():
categories_mock = [mock_model(Category)]
Category.stub_chain(:paginate, :order).and_return(categories_mock)
Note that this stubbing can cause problems if you integrate your views, because the pagination helper expects a pagination object and not an array.
Your test should be:
Category.should_receive(:paginate)
get :user_category, :per_page => 1, :page => 1
In order to have params[:per_page]
I assume you are using the will_paginate gem.
The paginate helper performs an ActiveRecord query on your database, in your case your test environment database.
The error you get means that no objects were returned by your paginate query.
This is likely due to the fact that your test should look like
Category.should_receive(:paginate)
get :user_category, :per_page => 1, :page => 1
in order to properly set your :per_page param.
Also, make sure your test db works fine and you actually have some categories in it.

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])

Why cant I do this in my controller

def search
#location = Location.find(params[:location_id])
start_date = DateTime.strptime(params[:start_date], "%m-%d-%Y")
end_date = DateTime.strptime(params[:end_date], "%m-%d-%Y")
#songs = #location.songs.find(:all, :conditions => {:play_date => start_date..end_date}).paginate(:page => params[:page], :per_page => 40)
render 'show'
end
Here is my error
undefined method `paginate' for #<Array:0x007fb00e49e6c8>
all works if i remove the will_paginate but i need it...any ideas or is there a better way to write this controller
Try writing
#songs = #location.songs.where(:play_date => start_date..end_date).paginate(:page => params[:page], :per_page => 40)
The difference? where returns an ActiveRelation object, while find retrieves all the matching objects in an array.
Hope this helps.
NoMethodError: undefined method `paginate' for []:Array
My will_paninate works perfectly but above error jumped out after upgrading to version 3.0.0.
Add following require will solve the issue:
require 'will_paginate/array'
Check out this post for the backward compatibility of will_paginate 3.0.
The will_paginate documentation states that combining .paginate with .find is not the way to go, because .find will load everything from your DB before .paginate has a chance to restrict the fetch.

Rails 2 NoMethodError in BrandsController#show undefined method `order' for #<Array:0x7065083ffd08>

I am getting a strange error in my local and production servers...
#search = Product.find(:all, :conditions => {:brand_id => #brand.id, :category_id => #category.id})
#search.order ||= :descend_by_price
#products = #search.all(params[:order] || :descend_by_price).paginate(:page => params[:page])
I am running rails 2.3.11 and Ruby 1.8.7 locally and on the production server. Any help would be very appreciated.
thanks
#search is an array of Product objects. You are calling order method on an array for which you are getting the error that says that the method order is not found for an array (probably order method is present only for one single Product but not for an array of Products).

Resources