Field::HasOne Added in 0.4.2

The HasOne association field wraps a has_one association on your model. It shows a link to the associated record on show pages and the record’s title on index pages. When there is no associated record, the show page links to creating one that is associated with the current record. has_one :through associations don’t get this link.

How to add a HasOne field

To add the basic HasOne field to a repository, return it as part of the #fields method:

def fields
  [
    Field::HasOne.new(:passport)
  ]
end

The associated model must have a repository.

Editing

The foreign key of a has_one association is stored on the associated record, so the field isn’t shown on edit and new pages. Change the association from the associated record, using Field::BelongsTo.

Not searchable by default. The field isn’t backed by a real column, so enabling it needs a searchable lambda - see Search for columns in an associated table.

Sorting

Not sortable by default. Pass a lambda to sortable to sort by a column in the associated table - see Sorting by columns in another table.

How to change the title of the associated record

The field displays the title of the associated record. To customize it, implement Repository#title, see repositories documentation for details.