2

The table(days_of_weeks) contains 8 columns , one is id, and rest constitues the days of week (sun,mon,tue ...) where the days of week holds either 0 or 1

I need to find rows whose sum of days of week is less than 3.

As a sql statement i can write

select id  from days_of_week where (mon + tue + wed + thu + fri + sat + sun ) < 3;

I'm trying to query it from rails

DaysOfWeek.select(:id).where((:sun + :tue + :wed + :thu + :fri + :sat + :sun) < 3)

it shows error because its just a symbol, i tried .to_i , but with no luck.

I know i can directly query with a sql statement from rails, but i want to do it the rails? way.

Any solution

4

2 に答える 2

5

ActiveRecordでは文字列条件を節に書くのが一般的です.whereが、以下は「SQL文」とみなされますか?

DaysOfWeek.select(:id).where('(mon + tue + wed + thu + fri + sat + sun ) < 3')
于 2013-06-21T07:00:15.303 に答える