Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Searching by age instead of date of birth. Is this possible? #182

Closed
yoavbo opened this Issue Dec 24, 2012 · 8 comments

Comments

Projects
None yet
6 participants

yoavbo commented Dec 24, 2012

Hi, I have a Users table where one of the colums is date_of_birth.
I have a method to calculate age from date of birth.
Would it be possible to set up a search according to age range and not date range, even though age is not a colum in the table?

I read it might be possible to do something like this with ransakcer or scopes, but I don't know how.

Using rails 3.2.8 and ransack 0.7.2.

Thanks and merry xmas

Member

radar commented Dec 24, 2012

Wouldn't age just be determined by a date range any way? If a person is 25 today then they would have been born between 1986-12-25 and 1987-12-25, yes? So why not do that calculation on your side and then pass these two dates as a range to Ransack?

On 25/12/2012, at 9:54, yoavbo notifications@github.com wrote:

Hi, I have a Users table where one of the colums is date_of_birth.
I have a method to calculate age from date of birth.
Would it be possible to set up a search according to age range and not date range, even though age is not a colum in the table?

I read it might be possible to do something like this with ransakcer or scopes, but I don't know how.

Using rails 3.2.8 and ransack 0.7.2.

Thanks and merry xmas


Reply to this email directly or view it on GitHub.

yoavbo commented Dec 25, 2012

That's correct, thanks for your reply.
So suppose I have a method called calc_age. The user has specified an age range of 25 to 50, so the dates would be
1986-12-25..1961-12-25. How do I pass this as a range to Ransack?

This is my current search form:

<%= search_form_for @search, url: search_users_path, method: :post do |f| %>
    <div class="field">
        <%= f.label :date_of_birth_gteq, "Age between" %>
        <%= f.text_field :date_of_birth_gteq %>
        <%= f.label :date_of_birth_lteq, "and" %>
        <%= f.text_field :date_of_birth_lteq %>
    </div>
<div class="actions"><%= f.submit "Search" %></div>
 <% end %>

As I understand it, with Ransack you can only use fields that are part of your tables, unless defined otherwise by ransacker. I don't quite know how to do this.

jwg2s commented Jan 14, 2013

@yoavbo Did you ever figure this one out? I'm attempting to do the same thing and am struggling a bit on this as well.

yoavbo commented Jan 14, 2013

I'm afraid I haven't, no.
Maybe someone here would be kind enough to explain.

jwg2s commented Jan 15, 2013

http://stackoverflow.com/questions/14021662/ransack-using-age-instead-of-date-of-birth

In Model:

scope :age_between, lambda{|from_age, to_age|
  if from_age.present? and to_age.present?
    where( :date_of_birth =>  (Date.today - to_age.to_i.year)..(Date.today - from_age.to_i.year) )
  end
}

For ransacker syntax:

ransacker :age, :formatter => proc {|v| Date.today - v.to_i.year} do |parent|
  parent.table[:date_of_birth]
end

In view:

<%= f.text_field :age_gteq %>
<%= f.text_field :age_lteq %>

ricsrock commented May 6, 2013

@jwg2s thanks so much for shedding some light on how Ransackers work. As you know, documentation is scarce.

For the purpose of my app, I need a Ransacker to return a BETWEEN clause, like your original named scope. Any suggestions for how I might construct a Ransacker that would do that?

My app uses named date ranges like "Year to Date" or "Last QTR" and coverts them to a start and end date and creates a between clause for the search. I'm having trouble converting these named scopes that I used in MetaSearch to Ransack style Ransackers.

jwg2s commented May 6, 2013

@ricsrock the example above is exactly what you're looking for I believe? Granted this does it for ages, so I first have to convert 2 dates to how old that makes a person, and then filter. Does the example above not help?

Hi. I've been facing a problem with the advanced search using ransack. I am using dateofbirth column in my db and defined a method to calculate the age. Now, I want to search a person's age using the predicate select. so any idea about how to get it working under this situation?

ransacker :age, :formatter => proc {|v| Date.today - v.to_i.year} do |parent|
parent.table[:date_of_birth]
end

Also when i use the ransacker method mentioned above. The "equals to" doesn't work and "less than" shows the greater than the age mentioned as value. Vice-versa for "greater than" predicate select.

@jonatack jonatack closed this Sep 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment