I'm having problems with relational data in simple form. So far "collection" is working well in the form, but I couldn't figure it out how to use "label_method" properly:
<%= f.input :participation_id, collection: Participation.where(user_id: current_user.id), as: :select, label_method: "????", label: 'Choose' %>
I would like to use the "examination" name from my "examination" model. I can retreive it from the model like this in the console:
p = Participation.where(user_id: 1)
p.first.examination.name
But as you might guess it is returning multiple values, because I have 3 different participations for the user with ID 1, and each participation has a different examination name.
How can I retreive each examination's name for each participation in the "label_method"?
Thanks.
new.html.erb =>
<%= simple_form_for(@order, html:{class: "well"}, :url => "https://somewhere.com", :method => :post) do |f| %>
<%= f.input :participation_id, collection: Participation.where(user_id: current_user.id), as: :select, label_method: "????", label: 'Choose' %>
<%= f.input :user_id, as: :hidden, input_html: { value: current_user.id } %>
<%= f.button :submit %>
<% end %>
My Models =>
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
has_one :order
end
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :participation
end
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
end
Database Structure =>
create_table "examinations", force: true do |t|
t.string "name"
t.string "shortname"
t.datetime "exam_date"
end
create_table "participations", force: true do |t|
t.integer "user_id"
t.integer "examination_id"
t.string "language_preference"
t.string "exam_center_preference"
end
create_table "orders", force: true do |t|
t.string "first_name"
t.string "last_name"
t.integer "participation_id"
t.integer "user_id"
end