0
以下是問題百思不得其解:無法通過=更新屬性和保存
我有一個模型字,它有一個屬性article_count
不過,如果我使用:
w = Word.first
w.article_count = 1
w.save
第一個字的article_count不會改變,日誌顯示:
Word Exists (0.4ms) ...
但我可以使用
w.update_attributes :article_count => 1
也許我誤解了保存的功能,有誰能給我解釋一下嗎?
=======================更新====================== =
這裏是我的文章型號:
class Article < ActiveRecord::Base
attr_accessible :content, :title, :source, :url
has_many :article_wordships, :dependent => :destroy
has_many :words, :through => :article_wordships
has_many :paragraphs, :dependent => :destroy
alias_attribute :word_count, :article_wordships_count
validates_uniqueness_of :title
end
這裏是我的代碼:
Article.select(:title).all.each do |a|
a.title = a.title.gsub(/\A.+?;\s(.+)/, '\1')
a.save
end
並運行代碼後,沒有在項目中更改。
這裏是控制檯信息:
1.9.3p194 :003 > a.save
(0.2ms) BEGIN
Article Exists (0.6ms) SELECT 1 AS one FROM `articles` WHERE (`articles`.`title` = BINARY 'A Shared State of Defeat' AND `articles`.`id` != 178) LIMIT 1
(0.9ms) UPDATE `articles` SET `title` = 'A Shared State of Defeat', `updated_at` = '2012-09-08 02:58:33' WHERE `articles`.`id` = 178
(2.2ms) COMMIT
=> true
都救也救不了!返回true。
奇怪的是,有時候它有用,有時它不起作用!
我惹毛了...
我更新了我的文章。此外,我發現問題可能在於.select(:title)? – HanXu