ruby on rails - Trouble writing action -
update
i have action in miniatures model called set_gold_and_silver
.
i want users model run when user destroyed, have before_destroy :set_gold_and_silver
in user model.
a user has many imagevotes. before destroy need delete imagevotes , run set_gold_and_silver
on miniatures imagevotes pertained to.
this i've got far , i'm getting undefined method 'miniatures'
.
it's not clear me whether caching self.imagevotes or whether deleted , error because no longer exist?
def set_gold_and_silver votes = self.imagevotes self.imagevotes.destroy votes.miniatures.uniq.each(&:set_gold_and_silver) end
my models
user
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
miniature
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
collection
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
you need make code simpler:
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
you have has_many :miniatures, through: :collections don't need work collections minuatures.
and code not working because still there before destroy. need done after, when depended user removed. , seems me need remove imagevotes in user destroy , set_gold_and_silver after that. it's not done, gold , silver stays.
Comments
Post a Comment