Devo interrogare i commenti fatti in un giorno. Il campo fa parte dei timestamp standard, è creato_at. La data selezionata proviene da una data_select. Come posso usare ActiveRecord per farlo?
Ho bisogno di qualcosa del genere:
"SELECT * FROM comments WHERE created_at BETWEEN '2010-02-03 00:00:00' AND '2010-02-03 23:59:59'"
Solo una nota che la risposta attualmente accettata è deprecata in Rails 3. Dovresti farlo invece:
Comment.where(:created_at => @[email protected]_date.end_of_day)
Oppure, se vuoi o devi usare condizioni di stringa pure , puoi fare:
Comment.where('created_at BETWEEN ? AND ?', @selected_date.beginning_of_day, @selected_date.end_of_day)
Personalmente avrei creato un ambito per renderlo più leggibile e riutilizzabile:
In Comment.rb, puoi definire un ambito:
scope :created_between, lambda {|start_date, end_date| where("created_at >= ? AND created_at <= ?", start_date, end_date )}
Quindi per interrogare creato tra:
@comment.created_between(1.year.ago, Time.now)
Spero che sia d'aiuto.
Questo codice dovrebbe funzionare per te:
Comment.find(:all, :conditions => {:created_at => @[email protected]_date.end_of_day})
Per maggiori informazioni dai un’occhiata ai calcoli del tempo
Nota: questo codice è deprecato . Usa il codice della risposta se usi Rails 3.1 / 3.2
Rails 5.1 ha introdotto un nuovo metodo di helper per date all_day
, vedere: https://github.com/rails/rails/pull/24930
>> Date.today.all_day => Wed, 26 Jul 2017 00:00:00 UTC +00:00..Wed, 26 Jul 2017 23:59:59 UTC +00:00
Se si utilizza Rails 5.1, la query sarà simile a:
Comment.where(created_at: @selected_date.all_day)
Ho eseguito questo codice per vedere se la risposta verificata ha funzionato, e ho dovuto provare a scambiare le date per farlo bene. Questo ha funzionato–
Day.where(:reference_date => 3.months.ago..Time.now).count #=> 721
Se pensi che l’output avrebbe dovuto essere 36, considera questo, Signore, quanti giorni sono da 3 giorni a 3 persone?
Comment.find(:all, :conditions =>["date(created_at) BETWEEN ? AND ? ", '2011-11-01','2011-11-15'])
Ho usato i 3 punti invece di 2. Tre punti ti offrono un intervallo aperto all’inizio e chiuso alla fine, quindi se fai 2 query per intervalli successivi, non puoi riprendere la stessa riga entrambi.
2.2.2 :003 > Comment.where(updated_at: 2.days.ago.beginning_of_day..1.day.ago.beginning_of_day) Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE ("comments"."updated_at" BETWEEN '2015-07-12 00:00:00.000000' AND '2015-07-13 00:00:00.000000') => # 2.2.2 :004 > Comment.where(updated_at: 2.days.ago.beginning_of_day...1.day.ago.beginning_of_day) Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE ("comments"."updated_at" >= '2015-07-12 00:00:00.000000' AND "comments"."updated_at" < '2015-07-13 00:00:00.000000') => #
E, sì, sempre bello usare un cannocchiale!
Se vuoi solo ottenere un giorno, sarebbe più semplice in questo modo:
Comment.all(:conditions => ["date(created_at) = ?", some_date])
Ci dovrebbe essere un comportamento di registrazione attivo predefinito su questo. È difficile interrogare le date, specialmente quando sono coinvolti i fusi orari.
Ad ogni modo, io uso:
scope :between, ->(start_date=nil, end_date=nil) { if start_date && end_date where("#{self.table_name}.created_at BETWEEN :start AND :end", start: start_date.beginning_of_day, end: end_date.end_of_day) elsif start_date where("#{self.table_name}.created_at >= ?", start_date.beginning_of_day) elsif end_date where("#{self.table_name}.created_at <= ?", end_date.end_of_day) else all end }
ci sono diversi modi. Puoi usare questo metodo:
start = @selected_date.beginning_of_day end = @selected_date.end_of_day @comments = Comment.where("DATE(created_at) BETWEEN ? AND ?", start, end)
O questo:
@comments = Comment.where(:created_at => @[email protected]_date.end_of_day)
Potresti usare sotto la gem per trovare i record tra le date,
Questa gem è abbastanza facile da usare e più chiara da star sto usando questa gem e l’API più chiara e la documentazione è anche ben spiegata.
Post.between_times(Time.zone.now - 3.hours, # all posts in last 3 hours Time.zone.now)
Qui puoi passare il nostro campo anche Post.by_month("January", field: :updated_at)
Si prega di consultare la documentazione e provarlo.