0

私はRailsが初めてです。多くの分類を持つカテゴリと会社のモデルがあります。私のログによると。コールバックが機能していないようbefore_updateです / ログを参照してください。更新後に呼び出されます。/。これが私の会社のモデルです:

class Company < ActiveRecord::Base

  attr_accessible :description, :name, :phone, :web, :fax, :email, :address, :category_ids

  has_many :categorizations
  has_many :categories, through: :categorizations

  before_create :increase_category_counters
  before_update :update_category_counters

  protected
    def increase_category_counters
      Category.update_all("count=count+1", ["id IN (?)", self.category_ids])
    end

    def update_category_counters
      old_categories = self.categories.collect {|c| c.id}
      puts "------------------------------ #{old_categories}"
      old_category_ids = old_categories-category_ids
      puts "------------------------------ #{old_category_ids}"
      unless old_category_ids.empty?
        Category.update_all("count=count-1", ["id IN (?)"], old_category_ids)
      end
      new_categories = category_ids-old_categories
      puts "------------------------------ CATEGORY_IDS: #{category_ids}"
      puts "------------------------------ OLD_CATEGORIES #{old_categories}"
      puts "------------------------------ NEW_CATEGORIES #{new_categories}"
      unless new_categories.empty?
        Category.update_all("count=count+1", ["id IN (?)"], new_categories)
      end
    end

  # Validations
  validates :name, :description, :phone, presence: true
  validates_uniqueness_of :name
  validates :email, format: {with: /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/}, allow_blank: true
  validates :web, format: {with: URI::regexp(%w(http https))}, allow_blank: true
  validates :fax, format: {with: /[0-9]/}, allow_blank: true

end

カテゴリ モデル:

class Category < ActiveRecord::Base

  attr_accessible :name, :count

  # Validations
  validates :name, presence: true

  has_many :categorizations
  has_many :companies, through: :categorizations
end

分類モデル:

class Categorization < ActiveRecord::Base

  belongs_to :category
  belongs_to :company

  validates_uniqueness_of :category_id, scope: :company_id
end

ここにログがあります:

Started PUT "/admin/companies/43" for 127.0.0.1 at 2012-05-07 20:39:28 +0800
Processing by Admin::CompaniesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"raDb5MWAViIOR6oYE5iJGNz8gio/WHM1H/mVYRHldxA=", "company"=>{"name"=>"ТЭДИ төв", "category_ids"=>["", "21", "22", "19"], "phone"=>"88574711", "email"=>"", "web"=>"", "fax"=>"", "address"=>"", "description"=>"<p>\r\n\tбы ыбө ыбө ыбө ыбөбыөыйб өыбөйыө</p>\r\n"}, "commit"=>"Update Company", "id"=>"43"}
  AdminUser Load (0.7ms)  SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 1 LIMIT 1
  Company Load (0.5ms)  SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT 1  [["id", "43"]]
  Category Load (0.5ms)  SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (21, 22, 19)
  Category Load (0.4ms)  SELECT "categories".* FROM "categories" INNER JOIN "categorizations" ON "categories"."id" = "categorizations"."category_id" WHERE "categorizations"."company_id" = 43
   (0.1ms)  BEGIN
  Categorization Exists (0.4ms)  SELECT 1 FROM "categorizations" WHERE ("categorizations"."category_id" = 22 AND "categorizations"."company_id" = 43) LIMIT 1
  SQL (30.8ms)  INSERT INTO "categorizations" ("category_id", "company_id") VALUES ($1, $2)  [["category_id", 22], ["company_id", 43]]
   (18.8ms)  COMMIT
   (0.1ms)  BEGIN
  Company Exists (0.8ms)  SELECT 1 FROM "companies" WHERE ("companies"."name" = 'ТЭДИ төв' AND "companies"."id" != 43) LIMIT 1
------------------------------ [21, 19, 22]
------------------------------ []
------------------------------ CATEGORY_IDS: [21, 19, 22]
------------------------------ OLD_CATEGORIES [21, 19, 22]
------------------------------ NEW_CATEGORIES []
   (0.2ms)  COMMIT

Rails 3.2.3 を使用しています。わかりません。私を助けてください?ありがとうございました。

4

1 に答える 1

3

update_allActive Record のコールバックや検証をトリガーしません。

http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-update_all

于 2012-05-07T13:00:21.513 に答える