2012-06-30 1 views
0

나는 Simple Form을 사용하고 있으며 2 개의 연결 값을 표시하는 방법을 사용하지 않습니다.두 모델의 선택 메뉴를 표시하는 방법은 무엇입니까?

가격은 서비스 또는 제품에 속할 수 있지만 동시에 둘 다 가질 수는 없습니다.

Price 
# service_id, product_id 
belongs_to :services # service.name 
belongs_to :products # product.name 
end 

대신 다음과 같이 내 간단한 형태의 모양을 갖는

<%= f.association :product, :input_html => { :class => "span5 } %> 
<%= f.association :service, :input_html => { :class => "span5 } %> 

내가 대신 하나 개의 필드로를 켜려고합니다.

simple_form_for의 방법은 무엇입니까?

보통 form_for은 무엇입니까?

답변

1

나는 더 좋은 방법은 다형성 연관을 사용하는 것이라고 생각합니다.

class Price 
    belongs_to :pricable, polymorphic: true 
end 

class Product 
    has_one :price, as: :priceable 
end 

class Service 
    has_one :price, as: :priceable 
end 

그런 다음, 형태, 당신은 사용할 수 있습니다 @priceable 제품이나 서비스입니다

<%= form_for [@priceable, Price.new] 

합니다.