0

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

1 Answer 1

1

Try This once:

p = Participation.where(user_id: 1)
<%= f.input :participation_id, collection: Participation.where(user_id: current_user.id), as: :select, label_method: lambda{|x| x.examination.name}, label: 'Choose' %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.