Question

I am new to rails so please point me in the right direction if I am not doing this in the rails way. I am using the carmen gem for a list of states and they aren't stored in the DB.

I followed this rails cast for using the tokenInput plugin, but my situation is a little different since the state id's aren't stored in the DB and I only have a has_many, not a has_many through relationship.

tokenInput expects JSON like [{"id": "id_here", "name":"name_val"}]. Since I don't have state ids in the DB, I manually create the JSON object in the filings controller by mapping the state code to the id key, and the state name to the name key (i.e. [{"id": "NC", "name":"North Carolina"}] )

Everything works great when I am adding states, but I need to delete states when the user removes one from the tokenInput on the edit page.

I can accomplish this by:

  1. Deleting every filing related to the campaign in the state_tokens setter method, then just create new ones.

  2. Adding code in the tokenInput onDelete callback to call the filing controller destroy method.

Which option would you implement and is there another way to do this that I am not thinking of?

Model:

class Campaign < ActiveRecord::Base

  has_many :filings
  attr_accessible :state_tokens

  def state_tokens
    self.filings.map {|r| {:id => r.state, :name => Carmen::Country.coded('US').subregions.coded(r.state).name}}.to_json
  end

  def state_tokens=(ids)
    state_codes = ids.split(",")
    state_codes.each do |s|
      Filing.find_or_create_by_campaign_id_and_state(self.id, s )
    end
  end

Controller:

class FilingsController < ApplicationController  
  def index   
    @filtered = Carmen::Country.coded('US').subregions.select {|r| r.name.downcase.include? "#{params[:q]}".downcase}
    respond_to do |format|  
      format.html  
      format.json { render :json => @filtered.map {|r| {:id => r.code, :name => r.name}}}
    end  
  end

end

Était-ce utile?

La solution

I decided to go with option 1, and just delete/replace the existing records. Let me know if you think there is a better way.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top