Skip to content

Conversation

adelevie
Copy link

WIP to demo the error when requiring bronto

@adelevie
Copy link
Author

error emanates from lib/rails_nlp.rb:

  def self.suggest_stopwords(n: stopwords_default, n_max: stopwords_max)
    whitelist = RailsNlp.configuration.stopwords_whitelist || []
    blacklist = RailsNlp.configuration.stopwords_blacklist || []
    limit = [n, n_max].min
    freq = Wordcount.group(:keyword_id).order('count_all DESC').limit(limit).count
    # ^ this line ^
    Keyword.where(id: freq.map(&:first)).pluck(:name) - blacklist + whitelist
  end

pry-rescue tells us:

From: /Users/alandelevie/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/relation/query_methods.rb @ line 997 ActiveRecord::QueryMethods#build_select:

     994: def build_select(arel, selects)
     995:   if !selects.empty?
     996:     expanded_select = selects.map do |field|
 =>  997:       columns_hash.key?(field.to_s) ? arel_table[field] : field
     998:     end
     999:     arel.project(*expanded_select)
    1000:   else
    1001:     arel.project(@klass.arel_table[Arel.star])
    1002:   end
    1003: end

repl'ing:

ArgumentError: wrong number of arguments (0 for 1)
from /Users/alandelevie/.rvm/gems/ruby-2.1.1/gems/sequel-3.48.0/lib/sequel/sql.rb:98:in `to_s'
[1] pry(#<RailsNlp::Wordcount::ActiveRecord_Relation>)> selects.first.to_s
=> "#<Arel::Nodes::Count:0x000001037d1ed8>"
[2] pry(#<RailsNlp::Wordcount::ActiveRecord_Relation>)> selects.second.to_s
ArgumentError: wrong number of arguments (0 for 1)
from /Users/alandelevie/.rvm/gems/ruby-2.1.1/gems/sequel-3.48.0/lib/sequel/sql.rb:98:in `to_s'
[3] pry(#<RailsNlp::Wordcount::ActiveRecord_Relation>)> selects.first.class
=> Arel::Nodes::Count
[4] pry(#<RailsNlp::Wordcount::ActiveRecord_Relation>)> selects.second.class
=> Sequel::SQL::AliasedExpression
[5] pry(#<RailsNlp::Wordcount::ActiveRecord_Relation>)> 

It appears that the pieces of the selects array, created inside of RailsNlp::suggest_stopwords (specifically freq = Wordcount.group(:keyword_id).order('count_all DESC').limit(limit).count), create an Arel::Nodes::Count and a Sequel::SQL::AliasedExpression without type-checking or noticing. ActiveRecord calls #to_s on the items in the selects array, but instances of Sequel::SQL::AliasedExpression have the weirdest #to_s definition:

# File lib/sequel_core/sql.rb, line 403
def to_s(ds)
  ds.aliased_expression_sql(self)
end

(source)

@adelevie
Copy link
Author

looks like we're not the first: rails/arel#133

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant