Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两个模型 - Question, Tag.
Question
Tag
我想对我的连接表count中的记录数做一个。questions_tags
count
questions_tags
如何从 Rails 控制台执行此操作?
假设您没有连接模型(只是一个连接表),您可以通过现有模型之一执行一些任意 SQL 以获取计数:
Question.connection.execute "select count(*) from questions_tags"
这将为您提供一个依赖于 db 的结果对象。对于 PostgreSQL,要从中获取实际整数:
Question.connection.execute("select count(*) from questions_tags").first["count"].to_i