Formtastic: Collection list with pagination or search? - ruby-on-rails

I've created a ProductPage model, which has many Products - what I'm currently trying to do in Active Admin is allow the admin user to, when they are creating a new ProductPage, select several products from a list of all available products. The trouble is that there are currently around 60k products, and attempting to load the page crashes the app.
The field is written like this at the moment:
f.input :products, :collection => Product.all
Is there anything out there that will paginate the results returned from that query? Alternatively, is there a way including a search field in place of the field, and have it AJAX in a handful of records?

You can try Select2 Gem. Docs.

Related

RailsAdmin with polymorphic associations

I am using, Rails 4.1, RailsAdmin 0.8.1 and Mongoid 5.0.1. I defined has_many / belongs_to relationship between Products and Pictures RailsAdmin generates a Product dropdown in the Picture model. I can choose Product or I can search for Product.
I implemented polymorphic association between Products, Pictures and Employees. Now when I edit Picture I have 2 dropdowns - to choose type (Product or Employee) and 2nd to choose the actual record.
The problem is RailsAdmin is trying to load the entire Employee or Product list into 2nd dropdown. I have many thousands Employees and eventually it timesout. Any ideas how to search?
The dropdown lists are populated when page is loaded. The second dropdown will have everything you send during page loading (before you have chosen 'type' in first dropdown).
What I think you want can be achieved using ajax to send a request when first dropdown is selected, retrieve the values and then populate your second dropdown list.
Some sources you can read. link1 link2 link3

Active Admin bogged down with relationship; how to customize?

I've got a Product class that has_many Events -- in most cases there can be thousands of saved events -- and I've no need to display these on the ActiveAdmin Product page but ActiveAdmin is trying to load them anyway, which makes the app crash.
How can I best have the Product admin page ignore the relationship? Something to do with customizing the collection? Putting an empty scope on the Event model and calling that as default on the admin page? Really unsure how to fix this.
See the answer here to either remove the filter from the index or scope the filter to a subset of options:
ActiveAdmin automatically loading full association table

Search Tags acts_as_taggable_on rails

I have created an app where advertising banners can be saved to a database and tagged with attributes, for example the dimensions of the banner (for example 300x250).
This has all been done using acts_as_taggable_on.
Now what I want to do is have a search box that when the user types in the dimension (for example 300x250) then my page will filter to show only the advertising banners tagged with '300x250' (this doesn't need to be done with Ajax or anything yet, a search button is fine)
I have currently installed the gem Ransack to search but can't figure out how to integrate it with Acts_as_taggable_on.
Is there an easy way for me to have a search box in my view that will search just through the tags and update the page with only the tagged objects?
Thanks so much in advance for any help you can offer!
That's easy. We just should write correct associations for ransack.
reflect_on_all_associations or reflections shows that Banner has an association named :taggable, which leads to model ActsAsTaggableOn::Tagging, which belongs_to ActsAsTaggableOn::Tag.
So then:
#q = Banner.ransack(taggings_tag_name_cont: 'interesting')
#banners = #q.result
Of course, be free using other matchers like eq, in and so on

ActiveAdmin -- How to create join table records?

I have two categories merchants and aliases who have a join table called aliases_merchants
In ActiveAdmin, how can I display a textbox whose content will get entered into the joining table?
I'm able to display the textbox but upon post it doesn't create the record in the joining table in the db.
Ideally, I would love to be able to get the functionality like the following (but I feel ActiveAdmin wouldn't be able to do it)...
I solved this by creating a sidebar and two member functions in the ActiveAdmin resource to accomplish this. Works just described above.

How can I have a single search field form that outputs all records from all models?

I want to create a really simple search form, basically a single field, that appears in the header of every page in my application.
My application has several models, each with several fields.
I'd like to know how to have that search get passed as a parameter into an output which shows all instances of that text item or items (meaning, if there are two elements, a first name and a last name, for example, that it pulls up records that have both values).
I currently use searchlogic, but I think that's really field specific. Looked into texticle since I am on heroku, but I still not clear what to do or if this is right way to go. Thank you.
Try out SOLR -- it will create an index of your models, and you can search on it. SOLR is the search engine -- use the following gems in rails: 'sunspot', 'sunspot_rails'
You'll do something like:
#sunspot_search = Sunspot.search User, Event, Coupon do |query|
query.keywords #search_query
query.with(:starts_at).greater_than Time.now
query.order_by(:starts_at, :asc)
query.paginate(:page => #page_number, :per_page => 200)
end
#sunspot_search will have your results array that you'll iterate over.
http://websolr.com/ gets you setup easily; works on Heroku.

Resources