Jak uzyskać rekord utworzony dzisiaj przez rails activerecord?

Jak napisać instrukcję warunkową, gdy chcę uzyskać wszystkie rekordy, które zostały utworzone dzisiaj?

Author: Wes Foster, 2010-05-27

9 answers

Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)

PS: ta odpowiedź została zmodyfikowana, ponieważ odpowiedź Harish Shetty była lepsza niż moja. Jak moja odpowiedź jest przyjęta jeden. I have updated this answer for community support

 185
Author: Mohit Jain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-03 21:17:37

Wiem, że to pytanie ma akceptowaną odpowiedź. Rozwiązanie zaproponowane w zaakceptowanej odpowiedzi może powodować problemy z wydajnością, gdy rozmiar tabeli rośnie.

Zazwyczaj, jeśli wykonujesz wyszukiwanie na podstawie kolumny created_at, Dodaj indeks do tabeli w pliku migracji.

add_index :posts, :created_at

Teraz do wyszukiwania rekordów utworzonych dzisiaj:

Szyny 3/4

Post.where("created_at >= ?", Time.zone.now.beginning_of_day)

Do wyszukiwania postów utworzonych w określonym dniu.

Post.where(:created_at => (date.beginning_of_day..date.end_of_day))

--------- lub -------------

Dodaj metodę statyczną do modelu

class Post < ActiveRecord::Base
  def self.today
    where("created_at >= ?", Time.zone.now.beginning_of_day)
  end
end

Post.today #returns posts today

Rails 2

Post.all(:conditions => ["created_at >= ?", Time.zone.now.beginning_of_day])

--------- lub -------------

Dodaj named_scope do modelu

class Post < ActiveRecord::Base    
  named_scope :today, lambda { 
    {
      :conditions => ["created_at >= ?", Time.zone.now.beginning_of_day]
    }
  }
end

Post.today #returns posts today
 112
Author: Harish Shetty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-29 16:26:34

MySQL:

Model.all :condition => ["DATE(created_at) = ?", Date.today] # rails 2
Model.where("DATE(created_at) = ?", Date.today) # rails 3

PostgreSQL:

Model.all :condition => ["created_at::date = ?", Date.today] # rails 2
Model.where("created_at::date = ?", Date.today) # rails 3
 28
Author: jigfox,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-05 09:45:25

Mohit Jain ' s answer adapted for Rails3

Model.where "DATE(created_at) = DATE(?)", Time.now
 15
Author: thekindofme,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-20 22:12:45

Model.rb

scope :posted_today, -> { posted_between_period(Time.now.midnight, Time.now.end_of_day) }

Posts_controller.rb

Post.posted_today
 6
Author: Parthiv,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-11 06:03:59

Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)

To" namescopes " atrybut z table_name.

 6
Author: Rafael Oliveira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-24 21:27:37

Z jakiegoś powodu żadne z innych rozwiązań w tym poście, ani inne na StackOverflow nie zadziałały dla mnie (używając Rails 4.2.4 i Ruby 2.2.3p173). Jest to jedyne zapytanie, które mogłem uzyskać do pracy z moją bazą danych Postgres:

Post.where("created_at >= TIMESTAMP 'now'")
 1
Author: Tim S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-06 10:32:01

Do odpytywania rekordów, które powstały od dzisiaj

Użyj scope z arel

class Post < ActiveRecord::Base    
  scope :create_from_today, -> {
    where(arel_table[:created_at].gteq(Time.zone.now.beginning_of_day))
  }
end

Użyj zakresu z surowym zapytaniem

class Post < ActiveRecord::Base    
  scope :create_from_today, -> {
    where('created_at >= now()')
  }
end

Wtedy możemy go użyć

today_posts = Post.created_from_today
 0
Author: Hieu Pham,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-21 05:48:06

W rails 4.2.3 aby uzyskać Rekordy utworzone dzisiaj, używając mysql użyj poniższego.

@usergoals = Goal.where("userid = :userid and Date(created_at) = :date", {userid: params[:id], date: Date.dzisiaj })

Tutaj używam wielu warunków, jeśli chcesz, możesz go edytować dla jednego warunku.

 -3
Author: santu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-12 06:38:33