2012-05-07 2 views
0

Я новичок в Rails. У меня есть категория и модель компании, которая имеет много категорий. По моему журналу. Я думаю, before_update callback не работает/см. Мои журналы. Он вызывается после обновления ./. Вот модель моя компания:ActiveRecord 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._%+-][email protected][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 

I» m, используя Rails 3. 2,3. Я понятия не имею. Пожалуйста, помогите мне? Спасибо.

ответ