Ruby on Rails - Add condition on ':include =>' to load limited number of objects

前端 未结 2 1721
南旧
南旧 2021-01-18 14:41

I have two models User and Event. The cardinality is one User has many Events. When I query the database to give me all the users and their corresponding events it returns t

2条回答
  •  清酒与你
    2021-01-18 15:26

    To my knowledge, you can't use the :include option in the way you describe.

    What is your problem with getting all the events anyway? You will have to load/join/query the events table anyway. You will need more memory, though, if you instanciate all events.

    Of course you could do two queries, one for the users with the events "today", and one for those without.

    You could also go the other way round:

    Event.find(:all, :conditions => "...only today...", :include => :user)
    

    Which would give you all events for today with users included. You can use another query to get all users without including the events, and do the rest in memory.

提交回复
热议问题