One of the most amazing things about Ruby on Rails is how easy it is to create Active Record associations between models. We try to keep the same simple approach in Avo too.
It's important to set the
inverse_of as often as possible to your model's association attribute.
Single Table Inheritance (STI)
When you have models that share behavior and fields with STI, Rails will cast the model as the final class no matter how you query it.
# app/models/user.rb class User < ApplicationRecord end # app/models/super_user.rb class SuperUser < User end # User.all.map(&:class) => [User, SuperUser]
For example, when you have two models,
SuperUser with STI, when you call
User.all, Rails will return an instance of
User and an instance of
SuperUser. That confuses Avo in producing the proper resource of
User. That's why when you deal with STI, the final resource
Avo::Resources::SuperUser should receive the underlying
model_class so Avo knows which model it represents.
class Avo::Resources::SuperUser < Avo::BaseResource self.title = :name self.includes =  self.model_class = ::SuperUser def fields field :id, as: :id field :name, as: :text end end
Link to child resource when using STI
Let's take another example. We have a
Person model and
Spouse models that inherit from it.
You may want to use the
Avo::Resources::Person to list all the records, but when your user clicks on a person, you want to use the inherited resources (
Avo::Resources::Spouse) to display the details. The reason is that you may want to display different fields or resource tools for each resource type.
There are two ways you can use this:
self.link_to_child_resource = trueDeclare this option on the parent resource. When a user is on the
Indexview of your the
Avo::Resources::Personand clicks on the view button of a
Personthey will be redirected to a
Spouseresource instead of a
field :peoples, as: :has_many, link_to_child_resource: falseUse it on a
has_manyfield. On the
Avo::Resources::Personyou may want to show all the related people on the
Showpage, but when someone click on a record, they are redirected to the inherited
Add custom labels to the associations' pages
You might want to change the name that appears on the association page. For example, if you're displaying a
team_members association, your users will default see
Team members as the title, but you'd like to show them
You can customize that using fields localization.