如何在Rails控制台中获取模型的所有记录的计数

时间:2021-05-30 22:50:45

I am new to Rails.

我是Rails的新手。

I have an ActiveRecord model called Post. I'd like to get count of all the records of the Post model. How can I accomplish this?

我有一个名为Post的ActiveRecord模型。我想了解Post模型的所有记录。我怎么能做到这一点?

Thanks in advance for your help.

在此先感谢您的帮助。

2 个解决方案

#1


3  

Try Post.all.count in your rails console if you are on Rails 4.

如果您使用的是Rails 4,请在rails控制台中尝试Post.all.count。

UPDATE:

Post.count is better as cristian mentioned

如所提到的那样,Post.count更好

#2


10  

Post.count

is what you are looking for.

是你在找什么。

#1


3  

Try Post.all.count in your rails console if you are on Rails 4.

如果您使用的是Rails 4,请在rails控制台中尝试Post.all.count。

UPDATE:

Post.count is better as cristian mentioned

如所提到的那样,Post.count更好

#2


10  

Post.count

is what you are looking for.

是你在找什么。