esmanbo
esmanbo

Reputation: 11

How can I get calculated value with different two column on PostgreSQL + Rails?

here is table

I would like to get sum of post_count and comment_count. What I did is like this, but does not work.

.select("count(tables.post_count + tables.comment_count) AS field_name").where("field_name >= ?", 3)

Upvotes: 0

Views: 79

Answers (1)

Benjamin Cbr
Benjamin Cbr

Reputation: 536

Try something like this

# Assuming your model is named Table
Table.
  select(:id, 'post_count + comment_count AS field_name').
  where('post_count + comment_count > 3') # Condition you added at the end of your question

This would return ActiveRecords with two fields:

  • the record id
  • the "field_name" column

But I'm not entirely sure this is what you are looking for.

Upvotes: 2

Related Questions