2014-03-24 88 views
0

UPDATE麻烦的写入动作

我有我叫set_gold_and_silver微缩模型的操作。

我希望我的用户模型在用户销毁时运行它,所以我的用户模型中有before_destroy :set_gold_and_silver

用户有很多Imagevotes。在销毁之前,我需要删除那些Imagevotes,然后在这些图片投票所涉及的所有缩图上运行set_gold_and_silver

这就是我到目前为止,我目前正在undefined method 'miniatures'

我不清楚我是否缓存self.imagevotes或者他们是否刚删除,然后我得到的错误,因为他们不再存在?

def set_gold_and_silver 
    votes = self.imagevotes 
    self.imagevotes.destroy 
    votes.miniatures.uniq.each(&:set_gold_and_silver) 
end 

我的模型

用户

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :confirmable 
    has_many :collections, dependent: :destroy 
    has_many :miniatures, through: :collections 

    has_many :imagevotes, foreign_key: "voted_id", dependent: :destroy 
    has_many :imagevotes, foreign_key: "voter_id", dependent: :destroy 
    before_destroy :set_gold_and_silver 

    def set_gold_and_silver 
    my_collections = self.collections.each 
    their_miniatures = collection.miniature.uniq 
    my_collections.their_miniatures.each(&:set_gold_and_silver) 
    end 


end 

微型

class Miniature < ActiveRecord::Base 
    has_many :collections, dependent: :destroy 
    has_many :users, :through => :collections 
    has_many :imagevotes, dependent: :destroy 

    def set_gold_and_silver 
    wipe = self.collections.all 
    wipe.each {|s| s.update_attributes :is_gold => false, :is_silver => false} 
    top_collections = self.collections.limit(4) 
    gold = top_collections.shift 
    gold.update_attribute :is_gold, true if gold 
    top_collections.each {|s| s.update_attribute :is_silver, true} 
    end 
end 

收藏

class Collection < ActiveRecord::Base 
    default_scope order('imagevotes_count DESC') 
    belongs_to :miniature 
    belongs_to :user 
    has_many :imagevotes, dependent: :destroy 

end 

Imagevote

class Imagevote < ActiveRecord::Base 
    belongs_to :collection, :counter_cache => true 
    belongs_to :voter, class_name: "User", :counter_cache => "voted_count" 
    belongs_to :voted, class_name: "User", :counter_cache => "vote_count" 
    belongs_to :miniature 

    after_create :set_gold_and_silver 
    after_update :set_gold_and_silver 

    def set_gold_and_silver 
     self.miniature.set_gold_and_silver 
    end 

end 
+1

你需要用模型的伪代码更新你的问题用户,微型,集合包括有/属性和过滤器之前/之后。你创建了一个单独的问题是很好的,但即使在那里也是如此,当然也是如此。 –

+0

编辑的问题。 – Ossie

回答

1

你需要让你的代码更简单:

class Miniature < ActiveRecord::Base 
    def set_gold_and_silver 
    self.collections.update_all("is_gold = false, is_silver = false") 
    top_collections = self.collections.limit(4) 
    gold = top_collections.shift 
    gold.update_attribute :is_gold, true if gold 
    top_collections.each {|s| s.update_attribute :is_silver, true} 
    end 
end 

class User < ActiveRecord::Base 
    def set_gold_and_silver 
    self.miniatures.uniq.each(&:set_gold_and_silver) 
    end 
end 

你的has_many:微缩模型,通过:集合,所以你不需要工作用集合来获得细节。

而现在你的代码不工作,因为一切仍然存在之前销毁。它需要在用户移除所有内容后完成。同样,对我来说,你需要在用户销毁和set_gold_and_silver后删除imagevotes。目前尚未完成,所以黄金和白银仍然存在。

+0

感谢代码简化。我认为,如果我做了after_destroy,我将无法找到需要set_gold_and_silver的User.collections? – Ossie

+0

因此,在这种情况下,您需要在before_destroy中查找属于用户的所有缩图,销毁属于我们用户的所有图片,并在其上调用set_gold。 –

+0

是的。对于第二步,图像投影将被自动销毁,因为它们是依赖的,并将被用户权利销毁?所以我只需要销毁前后的操作? – Ossie