Fill an association from another - ruby on rails

0

I have an application for scheduling services and I'm not able to populate a selectbox from the selection made in another. Explaining better: I have a selectbox with the available services and when I select a service, I want to fill the selectbox of professionals who have the same specialty of the service selected. In the professional class I have:

class Professional < ActiveRecord :: Base   belongs_to: specialty   has_many: reservations ...

in class Service:     class Service < ActiveRecord :: Base   belongs_to: specialty   has_many: reservations

in the Reserve class:

class Reserva < ActiveRecord::Base

belongs_to: client   belongs_to: service   belongs_to: professional

and in the new reservation view:

<%= simple_form_for @reserva, html: { multipart: true,
                                   class: 'form-horizontal' } do |f| %>
  

    <% = f.association: client, label: 'Client:', label_method:: name,                       value_method:: id% >     <% = f.association: service, label: 'Service:', label_method:: name,                       value_method:: id% >     <% = f.association: professional, label: 'Professional:',                       label_method:: name, value_method:: id% >     <% = f.input: price, label: 'Price:', readonly: true% >

My idea is, when selecting the service, list the professionals with the same specialty and fill the price field that has in the service. can anybody help me? Thank you.

    
asked by anonymous 21.01.2016 / 13:46

1 answer

0

Friend, let's go slowly. First, in the Professional model I think the most certain would be that the Professional has several or one (has_many or has_one) specialty (s) and not that it belongs to a specialty.

While your question, I understood that after I select the checkboxes my system would look for professionals who have that ability, right? For this I recommend using a search gem, such as PGSearch or others, depending on your bank.

link

Your professional model would look like:

class Profissional < ActiveRecord::Base
  include PgSearch

  has_many(has/one) :especialidades
  has_many :reservas

  pg_search_scope :search, against: [:name, :telephone(campos_do_seu_profissional)], :associated_against => {
    :habilidade => [:nome],
  }
end

Now if I use the Professional.search ("Mechanical") method, if there are any professionals that have the mechanical ability, it will return to you in this ActiveRecord.

I hope I have helped:)

    
27.01.2016 / 02:53