Reputation: 59
If I have the models:
class Bloque < ApplicationRecord
belongs_to :temporada
end
class Temporada < ApplicationRecord
has_many :bloques
end
And the collection_select in a view:
<%= collection_select(:bloques, :id, Bloque.all, :id, :temporada, {}, {class: 'form-control', multiple: 'true'}) %>
I want the name of temporada in Bloque, as in :temporada_name
, instead of plain :temporada in the collection_select
. Because the list appears as:
and I want the name of the temporadas to show.
Upvotes: 0
Views: 31
Reputation: 101811
Use Module#delegate
to delegate temporada_name
to temporada.name
.
class Bloque < ApplicationRecord
belongs_to :temporada
delegate :name, to: :temporada, prefix: true
end
<%= collection_select(:bloques, :id, Bloque.includes(:temporada).all, :id, :temporada_name, {}, {class: 'form-control', multiple: 'true'}) %>
Upvotes: 1