rails_admin affecting view ordering - ruby-on-rails

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) }

Related

Rails deletes record on edit and not submit

Having the following associations:
Workout has_many workout_sets through ...
workout_set has_many workout_steps through ...
When editing an object I'm experiencing the following issue:
1) go to /model/:id/edit
2) checking the db, once the page loads the top level attributes (the non-nested ones) are deleted from the db once the page loads, but the select marks as selected the correct values, as if in the moment of the load the data was correct.
3) leaving the page without saving (returning to /model/:id) does the following:
3.1) don't update the object, due to no form is submitted.
3.2) leave me with a model without it's primary properties, while the deepest nested attributes remain unchanged.
I'm using cocoon and simple_form to handle nested models. Is it something on Rails I'm missing out?
P.S: I can provide code if needed.
For the record:
The solution is as simple as:
:force_non_association_create => true on each link_to_add_association. For more information check this

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

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

Rails acts_as_soft_deletable with has_and_belongs_to_many

I have a problem with the acts_as_soft_deletable plugin and a has_and_belongs_to_many relationship.
I have a model "Place" which has a couple of Categories (like restaurant, hotel, etc). This means that a table "places_categories" is created in the database, containing two columns "place", and "category".
When I destroy a place, it is placed in the table "deleted_places" by acts_as_soft_deletable. Then I try to restore it and the application crashes because a place cannot exist without categories. The entries in "places_categories" that stored which categories the place belonged to are deleted as the place is deleted.
How can I make sure that "places_categories" does not remove the relations when a place is moved to the "deleted_places" table?
Since there is no option to explicitly preserve those entries - you could do something crazy and stupid and just override the delete_sql option to an empty String or somethings thats not going to fail on the "database-side" like so:
class Place
has_and_belongs_to_many :categories, :delete_sql => "select true"
end
This is untested ! Just an idea.
You can read about all available options here.

Rails Single Table Inheritance without "type" column

I'm porting some functionality to Rails, and I'm working with an existing table which is for comments.
Basically, there are two types of comments - profile comments (photo_id column is null) and photo comments (photo_id column is set to photo's ID)
I got single table inheritance working just fine by adding a type field to the table, but I'm wondering if there's a way to get my single table inheritance working without the type field. According to the Rails API documentation, "If you don‘t have a type column defined in your table, single-table inheritance won‘t be triggered. In that case, it‘ll work just like normal subclasses with no special magic for differentiating between them or reloading the right type with find."
I'm wondering if there's a way that I can customize my models to determine type based on photo_id being nil or having an integer value, rather than using the database column (which I'd rather not add if I don't have to.) Any ideas?
If comments models doesn't differ much, I wouldn't bother with single table inheritance at all. Just add:
# to Comment model
belongs_to :photo
belongs_to :profile
# to Profile model
has_many :comments
# to Photo model
has_many :comments
Then:
#photo.comments # will return comments associated with photos
#profile.comments # will return comments associated with profiles
There may be problem if you had both photo_id and profile_id set (I suppose it may happen when you comment a photo that is associated with profile), so you can change in Profile model:
has_many :comments, :conditions => "photo_id is not null"
Another approach (I think better) it to you polymorphic associations but you will need to modify you sql tables.
I suspect you cannot do this trivially. However, one possibility is to trick active record into using a view rather than a table, and write some database functions to set this magic attribute based on which id is set.
However, in the end, I suspect it would be far, far easier to just add the column.

Resources