activerecord - how to modify complex find_by_sql query w/ union into rails 3 -
here's current query:
@feed = ratedactivity.find_by_sql(["(select *, null queue_id, 3 model_table_type rated_activities user_id in (?)) " + "union (select *, null queue_id, null rating, 2 model_table_type watched_activities user_id in (?)) " + "union (select *, null rating, 1 model_table_type queued_activities user_id in (?)) " +"order activity_datetime desc limit 100", friend_ids, friend_ids, friend_ids])
now, bit of kludge, since there models set for:
class ratedactivity < activerecord::base belongs_to :user belongs_to :media end class queuedactivity < activerecord::base belongs_to :user belongs_to :media end class watchedactivity < activerecord::base belongs_to :user belongs_to :media end
would love know how use activerecord in rails 3.0 achieve same thing done crazy union have there.
it sounds should consolidate these 3 separate models single model. statuses such "watched", "queued", or "rated" implicit based on attributes of model.
class activity < activerecord::base belongs_to :user belongs_to :media scope :for_users, lambda { |u| where("user_id in (?)", u) } scope :rated, where("rating not null") scope :queued, where("queue_id not null") scope :watched, where("watched not null") end
then, can call activity.for_users(friend_ids)
3 groups trying accomplish above... or can call activity.for_users(friend_ids).rated
(or queued or watched) 1 group. way, of activity logic consolidated in 1 place. queries become simpler (and more efficient) , don't have maintain 3 different models.
Comments
Post a Comment