RailsAdmin with polymorphic associations - 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

Related

rails_admin affecting view ordering

Where do I define how ‘belongs_to’ records are listed in rails_admin dashboard? I want my 'belongs_to' record to be listed by id. Because that’s what is determining how my records are displayed in view despite using .order(:id 'ASC') in controller. Here are snap shots:
For example, when I load my records into the database and look in the database and the rails_admin dashboard, this is how same records are listed, i.e. in order of :id in the database but randomly in the rails_admin dashboard:order in db and dashboard
But when I look how they are displayed in view, even when I do entities.order(:id) in my code, they are displayed according to how they are listed in the rails_admin dashboard. Help will be appreciated. order in view
In your model, you would want to define the ordering on the has_many relationship.
has_many :children, -> { order(:id) }

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

Formtastic: Collection list with pagination or search?

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.

Ruby on Rails Model Relationships

I am very new to Ruby on Rails.
I am trying to set up a relationship between a user model and a model of ten different items.
My goal is to have users be able to check off items in the items model and then have the ones that have been checked off display on their profile.
I have used the Michael Hartl Ruby on Rails tutorial up to
the point of creating microposts.
Any tips on tutorials that will help me complete this would be greatly appreciated.
Thanks!
Basically, what you want is:
A User has_and_belongs_to_many :items
Also, an Item has_and_belongs_to_many :users
This is many to many relationship. Since, a user can has many items, and an item can belong to many users too. In rails, here has_and_belongs_to_many will implicitly create a table items_users which will contain id's of both, establishing the relationship.
Read more about this association here - http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association
Use checkbox tag for showing checkboxes for all the items. Documentation - http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
Based on whatever checkboxes are checked, save the records, establishing the relationship.
Done. :)
I don't know about other tutorials, if you've completed Hatel's then you have a very very good understanding of the rails framework as a whole. I would have an items_list model. Which had a user_id foreign key to associate itself with a user. Then I could have an items model which had an items_list foreign key to associate them to a list. Then items model could have a boolean field "active" or "checked" or whatever. Using these, and the associated relations, and some scopes, you can get what you want.
Just make sure to use the includes helper when you request this data, otherwise you'll easily get a N+1 problem.
http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations

Dropdown Box Ruby on Rails question

I am new to rails so go easy. I have two tables that I am trying to work with here, 'post' and 'category'.
The 'post' table includes the following columns, title:string content:text category:string.
The 'category' table simply contains name:string.
The idea is that the client can manage the categories and also when adding a new post, they can select from a drop down that references their categories.
What is the best way to accomplish this?
You might want to model the category differently. The usual approach is to create a PostCategory model and controller, and use a relation from posts to PostCategory. Read up on belongs_to and the other rails associations before you get much further into this project. When you're ready to continue, take a look at formtastic, it makes handling the forms for the associations much easier to code
flyfishr64 is right, the "correct" way to do this would be to put the categories in their own model/table.
There's lots of helpers like collection_select that will take your list of categories (PostCategory.all) and make a dropdown list for you with the appropriate name to save it in a specific field.
That said, you could pull a distinct list of the entries in that column already and use that for your dropdown, but it's a lot more hassle than just making a model for the category.

Resources