Guest User

Untitled

a guest
Jun 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. # I'm trying to convert this has_many from using :finder_sql to using :select and :conditions so that
  2. # I can use :include with it. Is there any way to FORCE the query rails generates to not append
  3. # that first AND????
  4.  
  5. EVENT_LIMIT = '20'
  6. RECENT_EVENTS_CONDITION =
  7. 'OR (actor_type = \'User\'
  8. AND actor_id IN (SELECT friend_id
  9. FROM relationships
  10. WHERE relationships.user_id = #{id} AND status=' + Relationship::ACCEPTED.to_s + ')'
  11.  
  12. has_many :recent_events_of_interest,
  13. :as => :subject,
  14. :class_name => "TimelineEvent",
  15. :select => 'timeline_events.*',
  16. :conditions => [RECENT_EVENTS_CONDITION],
  17. :order => 'timeline_events.created_at DESC LIMIT ' + EVENT_LIMIT
  18.  
  19. # This is the query that is generated... What I need is basically where I (the user) am the subject OR where
  20. # one of my friends is the actor.
  21.  
  22. SELECT timeline_events.*
  23. FROM `timeline_events`
  24. WHERE (`timeline_events`.subject_id = 5 AND `timeline_events`.subject_type = 'User' AND (OR (actor_type = 'User'
  25. AND actor_id IN (SELECT friend_id
  26. FROM relationships
  27. WHERE relationships.user_id = 5 AND status=0))) ORDER BY timeline_events.created_at DESC LIMIT 20
Add Comment
Please, Sign In to add comment