How to write this query with ActiveRecord or Mongoid?
-
Both of the interfaces for Mongoid and ActiveRecord are similar enough so I think either or will work. I need a query to meet the following criteria. I need to know the number of days a particular type of cereal has been eaten in the last 10 days. The cereal can be eaten 10 times in one day, but I only need to know that the day has had at least 1 time that the cereal was eaten. So if today is 11th, and a user has eaten Fruity Pebbles 2 times on the 5th, 10 times on the 7th, and once on the 8th, the answer is still 3. How does one perform a query similar to that with ActiveRecord or Mongoid? Assume the base class is 'Cereal' and there is a field named 'type'
-
Answer:
Not sure I understood. Cereal.where(:last_eaten.gt => Date.today - 10.day) or something like that in mongoid ? I'm going with my guts here, might not be completely correct
randombits at Stack Overflow Visit the source
Other answers
It's difficult to answer without a proper sample of your model. This could work if your model fits what I have in mind (similar to Katen's answer): Cereal.where("date > ?", Date.today - 10.days).select("distinct(date)").count You might have to tweak it a bit, but that's essentially what you have to do.
mbillard
Related Q & A:
- How to convert sql query to Hibernate Criteria query?Best solution by Stack Overflow
- How to write a complex query with doctrine?Best solution by Stack Overflow
- How to write Join Query for two tables without foreign key in Yii2?Best solution by Stack Overflow
- How to write a query for PHP and MySQL?Best solution by Stack Overflow
- How to write delete query with inner join?Best solution by codeproject.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.