2保存以使用友好ID更新我的别名

时间:2014-03-12 23:52:40

标签: ruby-on-rails ruby ruby-on-rails-4 friendly-id

我正在使用gem Friendly ID,我目前必须进行2次保存以保存slu ..

对我来说似乎不对。

class Contractor < ActiveRecord::Base
  include FriendlyId
  friendly_id :slug_candidates, use: :slugged, slug_column: :alias
  after_commit :generate_new_alias, unless: Proc.new {|contractor| contractor.business_name_changed? }


  def slug_candidates
      [
        :business_name,
        [:business_name, :city],
        [:business_name, :city, :state]
      ]
  end

  def generate_new_alias
    if self.alias != self.alias_was
      self.alias = nil
      self.save
    end
  end

end

知道我做错了吗?

0 个答案:

没有答案