Anyway to access a join table in Rails Console?

ruby-on-rails, ruby-on-rails-3

Solution

Assuming you have no join model (just a join table), you can execute some arbitrary SQL through one of your existing models to get a count:

Question.connection.execute "select count(*) from questions_tags"

This will get you a db-dependent result object. For PostgreSQL, to get an actual integer out of it with:

Question.connection.execute("select count(*) from questions_tags").first["count"].to_i

Problem

I have two models - `Question`, `Tag`. I would like to do a `count` on the number of records in my `questions_tags` join table. How can I do that from the Rails Console?

Original source