ransack

How to use Ransack Aliases

喜你入骨 提交于 2019-12-21 20:09:01
问题 I am trying to implement aliases with Ransack to make my URL search query shorter. According to the docs: https://github.com/activerecord-hackery/ransack#ransack-aliases class Post < ActiveRecord::Base belongs_to :author # Abbreviate :author_first_name_or_author_last_name to :author ransack_alias :author, :author_first_name_or_author_last_name end However when i use it in my model, i get a undefined method `ransack_alias' for #<Class:0x007f9376f176e0> 回答1: As I understand, ransack_alias

Ransack Search Results - to_xls?

三世轮回 提交于 2019-12-21 05:45:48
问题 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

Ransack: using age instead of date of birth

六眼飞鱼酱① 提交于 2019-12-21 05:15:21
问题 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

Rails + Ransack - Drop Down List Collection?

和自甴很熟 提交于 2019-12-20 10:29:00
问题 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

How o use Ransack with find_by_sql

本秂侑毒 提交于 2019-12-20 04:52:07
问题 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]) 回答1: 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

Activeadmin: how to filter for strings that match two or more search terms

你离开我真会死。 提交于 2019-12-19 06:04:48
问题 Let's say I've got User class with an :email field. And let's say I'm using activeadmin to manage Users. Making a filter that returns emails that match one string, e.g. "smith", is very simple. In admin/user.rb , I just include the line filter :email This gives me a filter widget that does the job. However, this filter doesn't let me search for the intersection of multiple terms. I can search for emails containing "smith", but not for emails containing both "smith" AND ".edu". Google tells me

How to set defaults for ransack sorting?

牧云@^-^@ 提交于 2019-12-14 04:16:38
问题 To put simply enough, I'd just like to know if there's a way to set defaults for the sorting functionality using the Ransack gem? ie. Currently, I have the following when the page loads: But instead, I'd like to have the following defaults when the page loads: Is it possible to do this via proper configuration of the Ransack Gem? Thanks in advance as always! Best Regards! 回答1: This works for me: @search = Change.search(params[:q]) @search.sorts = 'updated_at desc' if @search.sorts.empty? Note

Ransack sort by sum of relation

本秂侑毒 提交于 2019-12-14 03:57:33
问题 I'm using Ransack to sort a table of projects (model Project) by different properties, one of which is the total number of hours sold for a certain project (think consulting) which needs to be summarized from each Sale. Is there any way to achieve this? Relevant code below. class Project < ApplicationRecord has_many :sales, -> { order(:date) }, inverse_of: :project, dependent: :destroy end class Sale < ApplicationRecord belongs_to :project, inverse_of: :sales, touch: true validates :hours,

Rails Ransack - How to search multiple values

前提是你 提交于 2019-12-14 02:32:12
问题 Using the Ransack gem I was able to implement a search using checkboxes for certain values. However, this doesn't work with more than one checkbox. What am I missing? View: <%= search_form_for @search do |f| %> Part Time:<%= check_box_tag "q[job_type_cont]", value = "Part time" %></br> Full Time:<%= check_box_tag "q[job_type_cont]", value = "Full time" %></br> <%= f.submit "Search" %> <% end %> Index Action: def index @search = Job.search(params[:q]) @jobs = @search.result end 回答1: Your view

Adding Numeric Range Filtering to ActiveAdmin

为君一笑 提交于 2019-12-13 15:33:34
问题 Greetings and Happy Holidays 2015 -- I tried to add numeric range filtering per the excellent blog post by Boris Stall. I'm running: Rails 4.2.4 Ruby 2.2.3 ActiveAdmin 1.0.0pre2 I keep running into this error: Unable to find input class NumericRangeInput Here is my config/initializers/active_admin/filter_numeric_range_input.rb module ActiveAdmin module Inputs class FilterNumericRangeInput < ::Formtastic::Inputs::StringInput # Add filter module wrapper include ActiveAdmin::Inputs::Filters: