开发者

has_many :through - complicated model

开发者 https://www.devze.com 2023-04-11 02:02 出处:网络
I have a pretty complicated set of models and I\'m trying to figure out how to set it all up so that rails will generate chained queries (though I\'m not sure it is possible). So my question ends up b

I have a pretty complicated set of models and I'm trying to figure out how to set it all up so that rails will generate chained queries (though I'm not sure it is possible). So my question ends up being: how do I chain these together OR how do I properly paginate this type of function?

First, the high overview.

It is a "social network" and I have users who can be students or teachers. Students and teachers can both follow each other. Both can also do things on the site that result in activity_items. A students can belong to a teacher. Therefore, if a student is following a teacher, they should also see the activity_items of the students under that teacher.

And that is where it gets complicated. I am trying to figure out how to display an activity feed.

Now the models:

开发者_JAVA技巧
model ActivityItem < ActiveRecord::Base
  belongs_to :profile, :polymorphic => :true
  attr_accessor :posted_by_user
end

model Following < ActiveRecord::Base
  belongs_to :profile, :polymorphic => true
  belongs_to :following, :polymorphic => true
end

model Student < ActiveRecord::Base
  has_many :following_relationships, :as => :profile, :class_name => "Following"
  has_many :follower_relationships, :as => :following, :class_name => "Following"

  # getting the actual student/teacher model for each relationship
  def following
    self.following_relationships.reset
    following_relationships.collect(&:following)
  end

  def activity_feed
    all = following
    all.inject([]) do |result,profile|
      result | profile.activity_items
    end
  end

end

model Teacher < ActiveRecord::Base
  [same as student, really it is in an Extension]
end

So the method I'm most concerned about is activity_feed. Without running the large set of queries every time, I'm not sure how to paginate the data. I have a few more methods that go through each activity_item and set posted_by_user to true or false to let me know if it is an activity_item created by someone they are following directly or indirectly (through the teacher).

How can I modify this so that I'm able to either

(A) get a single (or two or three) queries so that I can do a pagination method on it.

(B) how would I properly cache / paginate a large data set like this?

0

精彩评论

暂无评论...
验证码 换一张
取 消