我想“计数器缓存”每个标签中的帖子数。后保存回调工作,但后破坏不是。看起来像销毁sql是不正确的。ActiveRecord - has_many:通过,:依赖=>:销毁sql是不正确的
class Post < ActiveRecord::Base
has_many :post_tags, :dependent => :destroy
has_many :tags, :through => :post_tags
end
class Tag < ActiveRecord::Base
has_many :post_tags, :dependent => :destroy
has_many :posts, :through => :post_tags
end
class PostTag < ActiveRecord::Base
self.table_name = :posts_tags
belongs_to :post
belongs_to :tag
after_save :update_tag_posts_count
after_destroy :update_tag_posts_count
def update_tag_posts_count
tag.posts_count = tag.posts.count
tag.save
end
end
测试失败
# @tag.posts_count == 10
Failure/Error: @tag.posts.first.destroy
ActiveRecord::StatementInvalid:
Mysql2::Error: Unknown column 'posts_tags.' in 'where clause': DELETE FROM `posts_tags` WHERE `posts_tags`.`` = NULL
正确的SQL应该是
DELETE FROM `posts_tags` WHERE `posts_tags`.`post_id` = {the post id}
尝试使用set_table_name而不是table_name =?我认为set_table_name方法改变了一些东西。 – 2012-03-28 15:58:28
@joe Pym这没有奏效。此外,set_table_name现在已被弃用。 - > DEPRECATION警告:不建议调用set_table_name。请改用'self.table_name ='the_name''。 – Marcelo 2012-03-29 15:03:59
我在我的情况中遇到了非常类似的错误。我也想要一个答案。 – user1149547 2012-07-18 00:08:15