ActiveRecord default where no longer works
dnagir opened this issue · comments
I have an existing Rails 3.1 app with complex scopes that use simple hash conditions like Article.published
.
It worked before I added squeel. The problem is that its .to_sql
started to generate incorrect SQL:
SELECT articles.* from articles.* WHERE articles.status = articles.approved
Not the WHERE clause and the approved
column that doesn't exist. It was specified on the scope:
Article.scope :published, where(:status => :approved)
I would prefer to keep existing AR queries and use squeel here and there.
Or maybe I'm just doing something wrong?
Also tried to config.load_core_extensions :symbol
, but it only seems compatibility with MetaWhere.
The value should be a string, not a symbol. That will fix the issue. Thanks!
Sent from my iPhone
On Nov 7, 2011, at 11:51 PM, Dmytrii Nagirniak
reply@reply.github.com
wrote:
I have an existing Rails 3.1 app with complex scopes that use simple hash conditions like
Article.published
.It worked before I added squeel. The problem is that its
.to_sql
started to generate incorrect SQL:SELECT articles.* from articles.* WHERE articles.status = articles.approvedNot the WHERE clause and the
approved
column that doesn't exist. It was specified on the scope:Article.scope :published, where(:status => :approved)I would prefer to keep existing AR queries and use squeel here and there.
Or maybe I'm just doing something wrong?Also tried to
config.load_core_extensions :symbol
, but it only seems compatibility with MetaWhere.
Reply to this email directly or view it on GitHub:
#75
No, that's not true for ActiveRecord. The value doesn't need to be a string. It only must respond to to_s
: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/predicate_builder.rb#L11
There are 2 problems though:
- I expected that squell not to change any existing ActiveRecord behaviour when there is no block given. It does change it as you can see (it works without squeel).
- I would probably be able to go and change symbols to strings everywhere, but that's quite a bit of code to change. And symbols represent much better what I need (predefined list of items, not an arbitrary string, - states).
I understand that this isn't the case for stock ActiveRecord -- this is the case for Squeel, however. See #67 for more details and discussion on this one.
Look, if there is no way of "enhancing" AR without breaking it. Then probably squeel should provide a "save" option.
I'm sure this topic will raise again and again.
I agree with guys there that "enhancement" should improve on something, not change. Also I don't see any reason to restrict the options to strings only. The whole Ruby works based on whether it responds to something or not.
So what I propose is to create and extension "safe" that would swap back the default implementation of AR/Arel.
But you can access squeel methods using something like Article.squeel.where
or Article.sq_where
or similar.
This should just leave normal AR methods alone, untouched.
Or maybe just use bang notion in "safe" mode: Article.where!
Think of it as jQuery.noConflict
-> Squeel.no_conflict
. It might be a golden spot.
I just encountered this problem and agree that we need a solution for this. Breaking standard AR functionality / behavior seems like a bad idea.