One of the most amazing things about Ruby on Rails is how easy it is to create Active Record associations (opens new window) between models. We try to keep the same simple approach in Avo too.
# Belongs to
field :user, as: :belongs_to
When you add a
BelongsTo association to a model, you will see three different field types.
On the Index view, you'll see a column with the
@title value of the associated model.
On the Show view, you'll see a link to the associated model.
On the Edit and Create views, you'll see a drop-down element with the available records. Here you may change the associated model.
To use a polymorphic relation you need to add the
class CommentResource < Avo::BaseResource self.title = :id field :id, as: :id field :body, as: :textarea field :excerpt, as: :text, show_on: :index, as_description: true do |model| ActionView::Base.full_sanitizer.sanitize(model.body).truncate 60 rescue "" end field :commentable, as: :belongs_to, polymorphic_as: :commentable, types: [::Post, ::Project] end
# Has One
HasOne association shows the unfolded view of you
HasOne association. It's like peaking on the Show view of that association. You also get the Attach/Detach button to easily switch records.
field :admin, as: :has_one
# Has Many
HasMany field is visible only on the Show page. Below the regular fields panel, you will see a new panel with the model's associated records.
field :projects, as: :has_many
Here you may attach more records by clicking the "Attach" button.
In a similar fashion, you may detach a model using the detach button.
# Has many through
HasMany association also supports the
field :members, as: :has_many, through: :memberships
# Has And Belongs To Many
HasAndBelongsToMany association works similarly to
field :users, as: :has_and_belongs_to_many