0

I have 2 models Person and Address with a has and belongs to many join table association.

Using will_paginate and ransack.

When filtering on Date field, the first page is filtered correctly. When I click on any of the other paginations link, query changes from date(persons.dob) >= '2013-09-01') to date(persons.dob) >= 2013)

class Person
  has_many :person_addresses
  has_many :addresses, through: :person_addresses
end

class Address
  has_many :person_addresses
  has_many :persons, through: :person_addresses
end

class PersonAddress
  belongs_to :person
  belongs_to :address
end

Persons' search form has dob_gteq, dob_lteq, name_cont and addresses.street_cont filters.

If I filter on dob_gteq only, the first page works correctly. All other paginations causes:

ActionView::Template::Error (PG::UndefinedFunction: ERROR:  operator does not exist: date >= integer
 LINE 1: ...name" WHERE ((date(persons.dob) >= 2013 AN...

Removing the addresses.street_cont filter from the search form allows it to work correctly and paginate as expected.


EDIT as requested controller action

@q = Person.search(params[:q])
@persons = @q.result(:distinct => true).paginate(:page => params[:page], :per_page => 30)

EDIT 2

Params coming in to search:

Parameters: {"utf8"=>"✓", "q"=>{"name_cont"=>"", "addresses_street_cont"=>"", "addresses_zipcode_cont"=>"", "dob_gteq"=>"09-01-2013", "dob_lteq"=>""}, "commit"=>"Filter"}
pcasa
  • 3,710
  • 7
  • 39
  • 67
  • Can you actually post the controller code that's generating this error? – SnakeWasTheNameTheyGaveMe Oct 23 '13 at 02:08
  • Done as requested, but its pretty standard. – pcasa Oct 23 '13 at 02:20
  • Both "search" and "result" methods are alien to me...is 'search' a method within the Person class that you haven't shown me? Why use result(:distinct => true) when you can use uniq? What version of RoR are you using? I'd gander that "operator does not exist: date >= integer" means that there's a comparison between a date object and an integer object...dates compare to dates. MOST importantly, where is the stack trace pointing to when the error is reported??? http://stackoverflow.com/questions/10130818/get-distinct-elements-from-a-rails-activerecord-query – SnakeWasTheNameTheyGaveMe Oct 23 '13 at 02:56
  • I'm sorry, should have posted links. Using will_paginate gem from https://github.com/mislav/will_paginate and ransack https://github.com/ernie/ransack. "search" is a ransack method. Updating error now. – pcasa Oct 23 '13 at 03:07
  • OK...let's clear up your models, first. Do you need has_many :through, or just has_and_belongs_to_many? If your're not doing anything with PersonsAddress except for storing the link between Person and Address, just use has_and_belongs_to_many. Take out your has_many declarations in each model. http://stackoverflow.com/questions/2780798/has-and-belongs-to-many-vs-has-many-through – SnakeWasTheNameTheyGaveMe Oct 23 '13 at 03:14
  • Also, now that I think about it...you need to check what the field persons.dob is giving to the params. How is it populated in the form? You basically need to also post your view. – SnakeWasTheNameTheyGaveMe Oct 23 '13 at 03:17
  • to answer the first, unfortunately I do need the join. Person can have more than 1 address and an address can have more than 1 person associated with it. For the second, I posted the actual params being passed in to the controller, easier to read. – pcasa Oct 23 '13 at 03:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39790/discussion-between-jakethesnake-and-pcasa) – SnakeWasTheNameTheyGaveMe Oct 23 '13 at 03:30

1 Answers1

0

In my case, changing the column from a datetime to a date, which fits better, solved my problem.

pcasa
  • 3,710
  • 7
  • 39
  • 67