----------
class FlaggedContent < ActiveRecord::Base
belongs_to :content, :polymorphic => true
end
----------
class Event < ActiveRecord::Base
default_scope where(:active => true).order("created_at DESC")
has_one :flagged_content, :as => :content
end
class Comment < ActiveRecord::Base
default_scope where(:active => true).order("created_at DESC")
has_one :flagged_content, :as => :content
end
# There are some other content types which can be flagged.
................
----------
每次创建一个"FlaggedContent"的时候,都会更新对应的content的active字段:
@flagged_content.content.update_attributes({:active => false})
这样操作以后,FlaggedContent就无法通过Rails内建的关联找到其对应的”content“了,因为相应的model中的
default_scope下已经找不到对应的record了。
我现在的处理是通过类似 @flagged_event = Event.unscoped.where(:id
=>@flagged_content.content_id).first来找的,Content类型多的时候这样写很不简洁。
不知道大家有没有好得办法来解决这个问题?
Thanks!