Creating a “feed” from multiple rails models, efficiently?

前端 未结 1 1763
谎友^
谎友^ 2020-12-29 13:54

This is a follow up to Creating "feeds" from multiple, different Rails models. In this question, tadman suggests this method of creating a user feed of recent item

1条回答
  •  别那么骄傲
    2020-12-29 14:42

    What I did once was, have a separate model Feed (feeds_controller) and update it in after_save callbacks to all the interesting models. So for example if you have a model Article, have an after_save callback:

    def after_save
      feed = Feed.new
      feed[:model_name] = 'Article'
      feed[:item_id] = id
      feed.save
    end
    

    then, you can access the feed linearly just like any other model. The computational expense is incurred when saving the the feed, not reading from the feed.

    Oh, you can also have Feed has_many :article; has_many :user, has_many :status and so forth, and then :include all those resources in the feed, and render them in views. Hope this makes sense ;-)

    0 讨论(0)
提交回复
热议问题