mardi 26 janvier 2016

Selecting a group collection of objects in Rails via coffeescript

I have an old Rails 3.2.22 app I'm maintaining and I'm running into a problem with creating a call object (model) and assigning a facility (model) by region (model).

Here is a breakdown of my models, they have been minimized to show the associations and important methods.

call.rb

class Call < ActiveRecord::Base
belongs_to :transferred_from, :foreign_key => :transfer_from_id, :class_name => 'Facility'
  belongs_to :transferred_to, :foreign_key => :transfer_to_id, :class_name => 'Facility'
  belongs_to :region
end

region.rb

class Region < ActiveRecord::Base
  attr_accessible :area
  has_many :calls
  has_many :facility_regions
  has_many :facilities, through: :facility_regions

  def active_facilities
    self.facilities.active
  end
end

facility.rb

class Facility < ActiveRecord::Base
  has_many :calls_transferred_from, :foreign_key => :transfer_from_id, :class_name => 'Call'
  has_many :calls_transferred_to, :foreign_key => :transfer_to_id, :class_name => 'Call'
  has_many :facility_regions
  has_many :regions, through: :facility_regions
end

When creating a call there is a list of regions which you can select one, and using coffee script it will only show the facilities which belong to that region using group collection select. Here is the excerpt from my _form.html.erb

<%= f.label :region %>
              <%= f.collection_select(:region_id, Region.order("area asc"), :id, :area, {:include_blank => true}, {:class => 'select'}) %></br>
              <%= f.label :Transfer_From %>
              <%= f.grouped_collection_select :transfer_from_id, Region.order(:area), :active_facilities, :area, :id, :facility_name_with_facility_address, {include_blank: true}, class: 'select' %><div id ="transfer-from-address"></div></br>

So the way it should work is a call is being filled out, a region selected (ie Houston), and the coffeescript changes the list of facilities based on the input of the region. So houston will show all houston facilities dallas will show all of dallas, etc etc. If you look at the group collection select I pass a method active_facilities which is in the region model which scopes all facilities belonging to the region and that are marked as active.

Here is the coffeescript I wrote

jQuery ->
  facilities = $('#call_transfer_from_id').html()

  update_facilities = ->
    region = $('#call_region_id :selected').text()
    options = $(facilities).filter("optgroup[label=#{region}]").html()

    if options
      # Set the options and include a blank option at the top
      $('#call_transfer_from_id').html("<option value=''></option>" + options)
      # Ensure that the blank option is selected
      $('#call_transfer_from_id').attr("selected", "selected")
    else
      $('#call_transfer_from_id').empty()

  $('#call_region_id').change ->
    update_facilities()

  update_facilities()

I've added a new region "Test" and assigned 3 facilities to it. But whenever I go to pull up that region "Test" it doesn't show the 3 facilities belonging to it but instead shows another region's facilities. This seems to be isolated to the new region only. I've tested assigning multiple regions to several facilities and when I create a call and select the proper region, those new facilities that were assigned to the region are scoped properly.

I went into the console and ran the following to make sure it's not an activerecord or association issue:

Region.last.active_facilities.count and received a count of 3 which is correct for the test region I built.

I'm not sure what the problem is here, I'm thinking it may be something with the coffeescript but am not 100% sure.

I'm sorry in advance if my question is fragmented or not clear. If you need any further code examples or explanation please feel free to ask.

Aucun commentaire:

Enregistrer un commentaire