Fill in an Association from another - ruby on Rails

Asked

Viewed 115 times

0

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

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

in the Service class: class Service < Activerecord::Base belongs_to :specialty has_many :reservations

in the Reserve class:

class Reserva < ActiveRecord::Base

belongs_to :client belongs_to :servico belongs_to :professional

and in the view of new booking:

<%= 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 :servico, 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 professionals with the same specialty and fill the price field that has in the service. Can someone help me? Thank you.

1 answer

0

Hey, buddy, let’s take it slow. First, in the model of Professional I think the most certain would be that the Professional has several or one(has_many or has_one) specialty(s) and does not belong to a specialty.

As for your question, I understood that after I selected the checkboxes my system would look for professionals who have that ability, right? For this I recommend using a search Gem, as Pgsearch or other, depending on your bank.

https://github.com/Casecommons/pg_search

Your professional model would look:

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 method.search("Mechanic"), if there are any professionals who have the mechanical skill, will return to you in this Activerecord.

I hope I’ve helped :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.