ransack

Ransack export results to CSV

北城以北 提交于 2019-11-30 14:47:19
I'm trying to export a list of Ransack ( Railscast ) results to a CSV file ( Railcast ). However, it keeps exporting all of the objects, instead of the results returned by the Ransack search. Can anyone tell me where I'm going wrong? In the Reports controller, I've tried passing both @bookings and @search.result: def index @search = current_user.bookings.search(params[:q]) @bookings = @search.result @search.build_condition respond_to do |format| format.html format.csv { render text: Booking.to_csv(@bookings) }\ end end And then the Booking to_csv method: def self.to_csv list CSV.generate do

Remove order from ActiveRecord scope

二次信任 提交于 2019-11-30 12:24:59
问题 I'm using rails ransack ( https://github.com/ernie/ransack ) to allow the users to filter and sort some records. I get the filtered and sorted records using traditional methods. @invoices = Invoice.search(params[:q]).result Now I would like to get some summary information so I have @invoices = Invoice.search(params[:q]).result @summary = @invoices.select("sum(balance) as balance_total").first Except when the user specifies a field to sort. I get the SQL error: Column "project_name" is invalid

Rails & Ransack - Sorting/searching based on a definition in the model

Deadly 提交于 2019-11-30 10:08:05
Say for example I have an event with start_date and length (as integer representing days). In the model I define end_date as start_date + length.days very simply as you would expect: def end_date start_date + length.days end All works fine in the template, I can use event.end_date to display the start date plus however many days length was set to, however , I want to now order the events by the end date using Ransack. The sort link for start_date looks like this: <%= sort_link @q, :start_date, "Start" %> If I try the same for end_date ( <%= sort_link @q, :end_date, "End" %> ) it unfortunately

Remove order from ActiveRecord scope

时光怂恿深爱的人放手 提交于 2019-11-30 02:36:43
I'm using rails ransack ( https://github.com/ernie/ransack ) to allow the users to filter and sort some records. I get the filtered and sorted records using traditional methods. @invoices = Invoice.search(params[:q]).result Now I would like to get some summary information so I have @invoices = Invoice.search(params[:q]).result @summary = @invoices.select("sum(balance) as balance_total").first Except when the user specifies a field to sort. I get the SQL error: Column "project_name" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY

Search multiple models at once with Ransack

时间秒杀一切 提交于 2019-11-29 23:10:13
I have a search form in the header of my app and I would like to use this search form to search through multiple models within the application. For example a request like /search?q=rails should trigger a search through multiple models like Work , Project , User and their defined attributes. I wanted to use Ransack because I already use it on the Work model in a different area of the app. I think I don't quite understand Ransack yet and the documentation always points out that you have to define @q = MyModel.search(params[:q]) to use it in the form search_form_for @q . Is there a way where you

Rails & Ransack - Sorting/searching based on a definition in the model

落花浮王杯 提交于 2019-11-29 14:49:34
问题 Say for example I have an event with start_date and length (as integer representing days). In the model I define end_date as start_date + length.days very simply as you would expect: def end_date start_date + length.days end All works fine in the template, I can use event.end_date to display the start date plus however many days length was set to, however , I want to now order the events by the end date using Ransack. The sort link for start_date looks like this: <%= sort_link @q, :start_date

Rails sorting associations with Ransack

和自甴很熟 提交于 2019-11-29 12:30:56
问题 first time poster. I am trying to sort a table of users using the Ransack gem and Kaminari for pagination. When I use name, id, etc. sorting works but when I try an association with posts_count, sorting breaks and won't work. Note: in the view, 'u.posts.count' work correctly. I have tried custom scopes in the users model, and creating custom objects for the search params but nothing seems to work. I think I am having trouble either in the default scope or the @search object not having the

Rails, Ransack: How to search HABTM relationship for “all” matches instead of “any”

北慕城南 提交于 2019-11-29 07:03:12
I'm wondering if anyone has experience using Ransack with HABTM relationships. My app has photos which have a habtm relationship with terms (terms are like tags). Here's a simplified explanation of what I'm experiencing: I have two photos: Photo 1 and Photo 2. They have the following terms: Photo 1: A, B, C Photo 2: A, B, D I built a ransack form, and I make checkboxes in the search form for all the terms, like so: - terms.each do |t| = check_box_tag 'q[terms_id_in][]', t.id If I use: q[terms_id_in][] and I check "A, C" my results are Photo 1 and Photo 2. I only want Photo 1, because I asked

Custom search with ransacker

随声附和 提交于 2019-11-28 22:24:08
问题 I'm trying to add a custom filter to ActiveAdmin which is powered by Ransack these days. Unfortunately, ransacker is not documented at all and from the few resources online I fumbled together the following (in the User model): ransacker :full_text, formatter: ->(search) { ids = User.search_in_all_translated(search).map(&:id) ids = ids.any? ? ids : nil } do |parent| parent.table[:id] end The search_in_all_translated method returns an array of users which match the search string across all

Ransack sort on count of HABTM or HMT associated records

自古美人都是妖i 提交于 2019-11-28 01:40:54
I have a HABTM relationship between the Theme and Quote models. The themes index view displays the count of quotes associated with each theme. I'd like to add a Ransack sort_link on that column, so the themes can be sorted by their count of associated quotes . I have done this successfully with has_many associations using a counter cache column, but Rails does not support counter cache columns for HABTM associations. So far, I've got a scope that adds a virtual attribute called quotes_count (by performing a single query, avoiding N+1) to the Theme model: scope :with_quotes_count, -> do joins(