Polymorphic association support added to searchlogic

If you have ever worked with polymorphic associations you know that using them in a search can be a pain in the ass. Rightfully so, because a polymorphic association is ambiguous, in order for ActiveRecord to do its magic with creating joins it needs more information that it can’t get. Such as, are you wanting to search type A or type B? In my projects I’ve been dealing with this by writing my own named scopes with my own joins. Not a big deal. But here’s the problem….

My favorite feature in Searchlogic is named scope delegation. I’ve noticed in a number of projects I’ve seen where programmers copy over named scope logic into related models. Which makes sense, because AR gives you no way of easily accessing related scopes. For example:

Order.named_scope :pending, :conditions => {:state => "pending"}
User.named_scope :pending_orders, :joins => :orders, :conditions => "orders.state = 'pending'"

Most people would look at that and never see anything wrong with it. What is the conditions for a pending order changes?

Order.named_scope :pending, :conditions => "orders.state = 'pending' AND approved = true

Now you have to remember to change this logic in associated models. This is a programming no-no. With searchlogic you don’t even need to touch the User model. You can access the order’s named scope, and it delegates that call:

User.orders_pending

Now when you change what defines an order as pending it will change everywhere in your application. This is great, but where this feature comes to a screeching halt is when you encounter a polymorphic association. This is because AR has no internal support for this either. For example, you can’t do:

Audit.belongs_to :auditable, :polymorphic => true
User.has_many :audits, :as => :auditable

Audit.all(:joins => :user)

Obviously the above won’t work. AR has no idea that there is a relationship with users through the polymorphic auditable relationship. To overcome this you have to write your own join. Again, there is nothing wrong with writing SQL, I don’t want to send the wrong message. The problem is that you are back to square one with my named scope delegation problem above. To solve this I added a way to overcome this. With searchlogic you could do this:

Audit.auditable_user_type_name_like("ben")

Searchlogic will take care of creating the join on the polymorphic relationship and then delegate the call to the User.name_like named_scope. I think it’s pretty nifty.

I hope I explained this well. I figured I would mention the new feature because I’m sure I’m not the only person that has encountered this problem.

  • Share/Save/Bookmark


6 Responses to “Polymorphic association support added to searchlogic”

  1. jDeppen says:

    Great thank you, I needed this.

  2. jDeppen says:

    I got this:
    searchlogic (2.4.0)

    but only got:
    binarylogic-searchlogic (2.3.5)

    gem install binarylogic-searchlogic –version “>= 2.4.0″ –source http://gems.github.com
    ERROR: could not find gem binarylogic-searchlogic locally or in a repository

  3. benjohnson says:

    Yeah github isnt hosting gems anymore. You should be using gem cutter. Update the ruby gems system and you should be good to go.

  4. jDeppen says:

    Okay thanks. FYI here’s what the readme says:

    Install the gem from rubyforge:

    sudo gem install searchlogic

    Or from github:

    sudo gem install binarylogic-searchlogic

  5. William T says:

    Thanks very much. This will help me in the next phase of my project.

  6. Ben,

    You did it again! Thanks, for this awesome feature! I was just figuring out how to make it work with searchlogic.