Using Arel for better chainability.
This is probably a know thing, But I had following scope:
scope :unfinished, where("status not in (?)", DatabaseServer.finished_states)
Which works. But when used in chain, such:
account.database_servers.unfinished.joins(:database_type => :server_type).all
it throws error:
"Amgiguous column status"
If you look closely you will see that query generated by unfinished
scope does
not have database_servers
prepended because SQL fragment is handrolled. Also accounts
table as well has a column called status
and hence the error when using the scope in a join.
The fix is to use Arel such that:
scope :unfinished, where(arel_table['status'].not_in DatabaseServer.finished_states)
Which generates proper query with database_servers
prefixed.