Would it be a performance hit to run that ASTTransformer? Yes, I agree that 
if it were to be implemented on #where, it should be implemented on #order 
and others. It just feels so natural for me that it works this way, 
especially coming from ActiveRecord :)

For example if I have something like this:

class Quiz < Sequel::Model
  plugin :table_select
  one_to_many :questions

  dataset_module do
    def newest
      order{created_at.desc}
    end


    def in(category)
      where(category: category)
    end


    def search(query)
      association_left_join(:questions).where {
        (quizzes__name =~ /#{query}/i) |
        (questions__title =~ /#{query}/i)
      }.distinct
    end
  end
end

As an outside user of the Quiz#search method, I have to remember that it 
does a join (it could have also been implemented as a subquery instead of a 
join). So when I'm using Quiz in controller, I have to remember to throw in 
#qualify before the #search (so that e.g. "created_at" column isn't 
ambiguous):

Quiz.newest.in(category).qualify.search(query)

I don't know if maybe I have a wrong approach.


On Saturday, March 28, 2015 at 1:36:23 AM UTC+1, Jeremy Evans wrote:
>
> On Friday, March 27, 2015 at 3:42:34 PM UTC-7, Janko Marohnić wrote:
>>
>> I'm not entirely sure what you meant. I would find it nice that, if I 
>> don't qualify a column, that it gets automatically qualified to the 
>> underlying model's table. This way I don't have to know if I'll be doing 
>> joins while building a query (I would rather not have to qualify column 
>> names if I don't have to). For example, I might do:
>>
>> Quiz.where(id: selected_quiz_ids).association_left_join(:questions).where
>> {...}
>>
>> When that query finishes, I will get an "ambiguous column 'id'" error. 
>> Even though I know that I meant "quizzes.id"; if I wanted `questions.id`, 
>> I would be natural to explicitly specify it. I found out now that 
>> "column_select" plugin actually does something else than this. Is there a 
>> plugin which does this automatically?
>>
>
> I'm not aware of one, and I don't plan to add one.  Handling this 
> generically (i.e. not just for hashes passed directly to #where, but for 
> subexpressions) would basically require running an ASTTransformer on all 
> inputs to #where and similar methods.  Handling this only for hashes passed 
> directly to #where would result in inconsistent behavior.
>
> Note that you can do:
>
>   Quiz.where(id: selected_quiz_ids).qualify.association_left_join(...)
>
> which will do what you want.
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to