0

Iam working in rails 3. i need count the row values from a mysql table.

In rails 3 i want to write that query in controller.

SELECT count(*) FROM #{table_name}

is that the correct query method.?

2
  • can you be more specific plz?? Commented May 8, 2013 at 6:45
  • 1
    You can get it by using the count method for that Model. For ex: if your model name is User then User.count will fetch the number of rows in users table. Commented May 8, 2013 at 7:19

1 Answer 1

2

You can count all the rows in a table easily with ActiveRecord. Let's use Post for your table name.

@posts = Post.all
@posts.count

Or like this:

Post.all.count
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.