Is there an approved way to add or register a new ARel visitor without 
adding it to the ARel source? Looking at the some of the source 
code<https://github.com/rails/arel/blob/master/lib/arel/visitors/to_sql.rb#L434-466>
 it 
appears that new visitors are just hard coded as a new methods or aliases.

The reason I'm asking is because I get  "TypeError: Cannot visit ClassName" 
when I override reader methods with some sort of composed object (similar 
to Active Record's composed_of) and use that attribute in a uniqueness 
validation. 

Take the following for example:

class Cat < ActiveRecord::Base
  validates :name, :uniqueness => { :scope => [:breed] }

  def breed
    CatBreed.new(super)
  end
end

class CatBreed
  def initialize(breed)
    @breed = breed
  end

  def to_s
    @breed
  end
end

The above code will raise "TypeError: Cannot visit CatBreed" when I try to 
create a new cat unless I add a visitor method to Arel::Visitors::ToSql 
called visit_CatBreed:

class Arel::Visitors::ToSql
  def visit_CatBreed(value)
    quoted(value.to_s)
  end
end

I'm hoping there is a better way to deal with this problem, either with a 
less hacky ARel approach or something different all together.


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-core/-/rKzxKCppeZcJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to