Thursday 15 May 2014

Rails: Simple Form Association label_method from Grandparent -


I am trying to create a form that will create a new record using simple form, but show me the correct label First of all, the relevant model in the dropdown list is:

  class service & lt; ActiveRecord :: Base is_to: Business has_one: Nomination has_many: Clients, through: Nomination end class clients & lt; ActiveRecord :: Base is_to: Business has_one: Nomination has_many: Services, through: Nomination end class nomination & lt; ActiveRecord :: Base is_to: service is_to: customer has_many: jobs end class jobs  

The basic idea is that the customer will be enrolled in one or more services, representing an appointment to the job service. When creating a new job, I have to choose the enrollment related to the job. From Html.erb:

  & lt;% = f.association: Nomination, label_meter :: service_id, value_method :: id, prompt: 'Named service'% ' Select.  

This works, but it only shows me service_id from the enrollment table. What I want to see, the name of the client (fname and lname) and the name of the service contained in the dropdown list, such as: "John Do: Window Washing" is the problem that both come from parents of enrollment. Basically I have to face two organizations that I want to go to on that label.

I thought that about D-generalization is so much that I have the necessary data in the enrollment record, but I did not want to do this. / P>

Any thoughts?

Define the following method in the enrollment category:

  def name "# {Client.full_name}: # {service.name}" end  

Then you should be able to use this method in your form:

 < Code> & lt;% = f.association: enrollment, label_method :: name, value_method :: id, prompt: 'named service'% ' Select  

2 * n + 1 to avoid sql questions to prepare enrollment collections with included customers and services.


No comments:

Post a Comment