2014-05-22 3 views
0

간단한 형식의 관계형 데이터에 문제가 있습니다. 지금까지 "컬렉션"형태로 잘 작동,하지만 난 제대로 "label_method는"사용하는 방법을 알아낼 수 :간단한 양식 - 관계형 데이터를 레이블 방법으로 사용하는 방법

<%= f.input :participation_id, collection: Participation.where(user_id: current_user.id), as: :select, label_method: "????", label: 'Choose' %> 

나는 내 "검사"에서 "검사"이름을 사용하고 싶습니다 모델. 나는 콘솔에서이 같은 모델을 retreive 수 있습니다

p = Participation.where(user_id: 1) 
p.first.examination.name 

하지만 당신은 내가 ID 1과 사용자에 대한 3 명 가지 참여가 있기 때문에, 여러 값을 반환 추측하고 있습니다 각 참여는 다른있다 시험 이름.

은 어떻게 "label_method"각 참가 각 검사의 이름을 retreive 수 있습니까?

감사합니다.

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 %> 

내 모델 =>

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 

데이터베이스 구조 =>

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

한 번이 시도 :

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' %> 
+0

대단히 감사합니다! – msdundar

관련 문제