marcamillion
marcamillion

Reputation: 33775

Anyway to access a join table in Rails Console?

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?

Upvotes: 8

Views: 3956

Answers (1)

Jim Stewart
Jim Stewart

Reputation: 17323

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

Upvotes: 5

Related Questions