Frage

I'm having hard time with this, it's not a direct problem of implementation but I don't understand which is the right way to do it, I have two options, but first, these are my models:

class Boat < ActiveRecord::Base
  validates :name, presence: true, uniqueness: { case_sensitive: false }

  has_many :tech_specs, order: 'position'

  def visible?
    self.visible
  end
end

class TechSpec < ActiveRecord::Base
  validates :boat_id, presence: true
  validates :tech_spec_name_id, presence: true, uniqueness: { scope: :boat_id }

  belongs_to :boat
  belongs_to :tech_spec_name

  before_destroy :destroy_name_if_required

  acts_as_list scope: :boat

  def as_json(options = {})
    super(options.except!(:tech_spec_name_id).merge!(methods: [self.name]))
  end

  def name
    self.tech_spec_name.try(:name) || ''
  end

  def name=(value)
    self.tech_spec_name = TechSpecName.find_or_create_by_name(value)
  end

  def destroy_name_if_required
    self.tech_spec_name.destroy if self.tech_spec_name.tech_specs.size <= 1
  end
end

class TechSpecName < ActiveRecord::Base
  validates :name, presence: true, uniqueness: { case_sensitive: false }

  has_many :tech_specs

  def self.with_name_like(str)
    where('lower(name) LIKE lower(?)', "%#{ str }%")
  end
end

The problem is that I want a page for a boat showing some tech specs when with a locale and when on a different locale, showing other tech specs.

Idea #1

My basic idea is to add to TechSpec globalize3 on tech_spec.value and on TechSpecName for field tech_spec_name.name

Idea #2

The other idea is to remove TechSpecName and, instead, use a field (tech_spec.name) that will "replace" completely TechSpecName. Notice that in this case, I'll still need to fetch names for autocomplete, but I will filter them in TechSpec instead of fetching all from TechSpecName. This field will use globalize3 again obviusly.

I don't know the downside of both approaches, so I need a suggestion.

War es hilfreich?

Lösung

Seems like idea #1 is ok, it works correctly and reduce the amount of repeated text inside Db.

I18n.with_locale helps a lot too, also Globalize.with_locale is helpful

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top