ransack

How to search array through ransack gem?

核能气质少年 提交于 2019-12-03 20:30:03
问题 I'm using ransack gem for searching in rails application. I need to search an array of email_ids in User table. Referring to this issue at ransacking, I followed the steps and added this to the initializers folder ransack.rb Ransack.configure do |config| { contained_within_array: :contained_within, contained_within_or_equals_array: :contained_within_or_equals, contains_array: :contains, contains_or_equals_array: :contains_or_equals, overlap_array: :overlap }.each do |rp, ap| config.add

Ransack: using age instead of date of birth

╄→尐↘猪︶ㄣ 提交于 2019-12-03 17:27:29
I would like to use ransack to build an advanced search function for a page with Users . I have a small method to calculate age from date of birth: def age(dob) now = Time.now.utc.to_date now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1) end That works on normal display ( as in age(@user.date_of_birth) ) But when using search_form_for I cannot do the same: <%= 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 %

Ransack Search Results - to_xls?

自闭症网瘾萝莉.ら 提交于 2019-12-03 17:20:18
I have a ransack search form which is working wonderfully, I would like to add an export for the user to send the contents of the result set to an XLS file. I have implemented the to_xls sucessfully as well, however it is giving me back the fullest possible scope of the object I am searching, and not the filtered results that are shown in the view. def index @search = Expense.search(params[:q]) @expense_list = @search.result.sort_by(&:expense_date) respond_to do |format| format.html format.xml { render :xml => @expense_list } format.xls { send_data @expense_list.to_xls, :filename => '123.xls'}

Ransack: How to use existing scope?

末鹿安然 提交于 2019-12-03 16:14:12
问题 Converting a Rails 2 application to Rails 3, I have to replace the gem searchlogic. Now, using Rails 3.2.8 with the gem Ransack I want to build a search form which uses an existing scope. Example: class Post < ActiveRecord::Base scope :year, lambda { |year| where("posts.date BETWEEN '#{year}-01-01' AND '#{year}-12-31'") } end So far as I know, this can be achieved by defining a custom ransacker. Sadly, I don't find any documentation about this. I tried this in the Post class: ransacker :year,

How can I save Ransack searches to the database?

拥有回忆 提交于 2019-12-03 09:06:33
I'm trying to save Ransack searches to the database. I believe I should be able to just store the params[:q] value, then append that to the search URL when I want to recall the search. I don't know how to save the params[:q] value, though. The URL that Ransack creates is something like this: http://site.com/search?utf8=%E2%9C%93&q%5Bone%5D=something&q%5Btwo%5D=&q%5Bthree%5D=&q%5Blow_number%5D=0&q%5Bhigh_number%5D=300000&q%5Bfour%5D=&commit=Search My route to the action that will save the search is: match 'users/:id/saved_search_add' => 'users#saved_search_add', :as => :saved_search_add If I

Ransack: How to use existing scope?

喜你入骨 提交于 2019-12-03 05:31:42
Converting a Rails 2 application to Rails 3, I have to replace the gem searchlogic . Now, using Rails 3.2.8 with the gem Ransack I want to build a search form which uses an existing scope. Example: class Post < ActiveRecord::Base scope :year, lambda { |year| where("posts.date BETWEEN '#{year}-01-01' AND '#{year}-12-31'") } end So far as I know, this can be achieved by defining a custom ransacker . Sadly, I don't find any documentation about this. I tried this in the Post class: ransacker :year, :formatter => proc {|v| year(v) } But this does not work: Post.ransack(:year_eq => 2012).result.to

Setting default search parameter on Ransack for rails

ε祈祈猫儿з 提交于 2019-12-03 03:17:25
I've been wracking my brain over this but can't get it. I feel like the answer is probably obvious. What I'm trying to do is the following: I have an index controller which lists a series of Jobs which I can search using Ransack. Each job has a completion date which either has a date in it or is null (unfinished). Currently, the search itself works great. I would like to make it so that the index page loads up showing only the unfinished work, but I also want it to work so that when someone does run a search, returns results for both finished and unfinished work. Any help would be greatly

Rails + Ransack - Drop Down List Collection?

走远了吗. 提交于 2019-12-02 22:43:29
I am loving the ransack gem for its flexibility, however I am unable to get the standard collection_select to function properly. Perhaps someone can assist. Example: <%= collection_select(:expense, :project_id, Project.all, :id, :name, { prompt: 'Select Project'}, { class: 'span4' }) %> in this case, this code is from an expense entry screen, so the first parameter is the expense object. What should it be in the ransack form? Also, I know I need to get the suffix in there. In this example, I would like project_id_eq to be the search pattern. Also, my form is on a controller and view called

Rails filter with button or link_to using Ransack

自作多情 提交于 2019-12-02 13:32:00
问题 I am developing a rails application using the Ransack gem and below is the code that I have written so far to filter my database which works like a charm. Now what I am trying to do is to add additional button like filter options to my index view (where each button has pre-defined filter value). In other words, once the database is first filtered with a brand name, then I would like users to be able to further filter the database by clicking one of the buttons which has a pre-defined filter

How o use Ransack with find_by_sql

限于喜欢 提交于 2019-12-02 07:27:36
How can I use Ransack with "find_by_sql"? I generally do this: def index @m = Mymodel.ransack(params[:q]) @mymodels = @m.result.page(params[:page]) end Can I use Ransack when doing a custom query?: @mymodels = Mymodel.find_by_sql(["SELECT ... ", current_user]) If I get it correctly, this is want you want: def index @m = Mymodel.ransack(params[:q]) @mymodels = @m.result.page(param[:page]).where(user: current_user) end To answer your question directly though, you can convert it to an SQL string, but you won't (or would be hard) be able to use current_user as an argument: def index @m = Mymodel