specify values in Simple Form Association

2019-09-03 02:29发布

问题:

I am trying to select specific record but when trying to save , employee_id is saved with a null value, and without putting any condition it works normally , is there any way to solve this ?

<%= f.association :employee , collection:
Employee.where('student_advisor' => true).map(&:full_name) %>

回答1:

This is what works for me:

<%= f.association :employee, :collection => Employee.where('student_advisor' => true), :label_method => :full_name, :value_method => :id %>

Though IMHO its better to set that query in your controller to keep the logic out of your views...

So in the controller:

@employee_list = Employee.where('student_advisor' => true)

And in the view:

<%= f.association :employee, :collection => @employee_list, :label_method => :full_name, :value_method => :id %>