#73 ✓resolved
Tomash

Enable custom :joins OR let overwrite :joins

Reported by Tomash | March 25th, 2009 @ 06:55 AM

Hi!

My problem is a result of a pretty complex application/db logic, but I'm going to describe it to prevent comments like "this can be done other way" ;)

To put it shortly: models Image and Video are also accessible as Multimedium model, which is a normal model < AR::Base. The thing is that multimedia isn't a "real" (i.e. data-keeping) table in the DB, instead being a VIEW:

'CREATE VIEW multimedia AS 
      SELECT id, user_id, title, access, filename, created_at, updated_at, "Image" AS media_type FROM images 
      UNION ALL 
      SELECT id, user_id, title, access, filename, created_at, updated_at, "Video" AS media_type FROM videos;'

This way I can easy access all media altogether, paginate and sort this hybrid etc. etc.

I'm also using acts_as_taggable_on_steroids (patched a bit, but irrelevant here I think) to handle tagging. Now it uses a polymorphic association to know the class/model that is tagged through tagging join-model (tag_id, model_id, model_type).

This poses one problem: trying to find all the media with one given tag.

If I use Searchlogic's functionality, i.e.

@search = Multimedium.new_search(
      :conditions => {
        :user_id => @user.id,
        :tags => ({:id => @tag.id} if @tag)

It joins the "tags" table, but giving 'Multimedium' as the tagging#model_type, thus resulting in 0 results (there is no tagging with model_type 'Multimedium', only 'Image' and 'Video'). I want to add a custom join:

@search.find(:all, :joins => "INNER JOIN taggings ON taggings.taggable_id = multimedia.id AND taggings.taggable_type IN ('Image', 'Video') INNER JOIN tags ON tags.id = taggings.tag_id")
This join works very well, i.e. returns all the Multimedia for a given tag when I use it without all the searchlogic awesomeness (as I'm forced to do now):
if @tag
      Multimedium.find(:all, :joins => "INNER JOIN taggings ON taggings.taggable_id = multimedia.id AND taggings.taggable_type IN ('Image', 'Video') INNER JOIN tags ON tags.id = taggings.tag_id", :conditions => "tags.id=#{@tag.id}" )
    end
But Searchlogic overwrites my :joins parameter, as it creates own :joins => [:tags] that I cannot empty/remove/modify (tried hardly) and results in a "wrong" join (the one aiming for tagging#model_type='Multimedium').

So, I'd basically like to pass a custom :joins parameter to MultimediumSearch to get all the Multimedia with a given tag.

Comments and changes to this ticket

  • Ben Johnson

    Ben Johnson March 30th, 2009 @ 11:53 AM

    • State changed from “new” to “resolved”

    I see what you are saying, but I don't think searchlogic can do this. If you removed searchlogic, in order to do this in AR, you would have to explicitly write all joins, since AR supports nested conditions. Sorry.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Provides common named scopes and object based searching.

People watching this ticket

Pages