#103 ✓moved_to_github

Problem with Multiple Conditions on STI Models

Reported by omarvelous | July 21st, 2009 @ 11:14 PM

The following is a result of the following example search condition


SELECT count(*) AS count_all FROM rides INNER JOIN make_models ON make_models.id = rides.model_id AND (make_models.type = 'Model' ) INNER JOIN make_models makes_rides ON makes_rides.id = rides.make_id AND (makes_rides.type = 'Make' ) WHERE ((((rides.deleted_at IS NULL) AND (make_models.name LIKE '%Tahoe%')) AND (make_models.name LIKE '%Chevrolet%')) AND (rides.deleted_at IS NULL))

I currently have Makes and Models as subclasses to MakeModels, which is self-referential using nested_sets. So

Make has_many Models
Model belongs_to Make

Now... I don't think this is necessarily a "bug".... but how would one go about changing this

(make_models.name LIKE '%Tahoe%')) AND (make_models.name LIKE '%Chevrolet%')

to this with OR which will fix my dilemma (and hopefully any one else)

(make_models.name LIKE '%Tahoe%')) OR (make_models.name LIKE '%Chevrolet%')

Comments and changes to this ticket

  • Ben Johnson

    Ben Johnson July 30th, 2009 @ 02:51 AM

    • State changed from “new” to “open”

    You could do:

    Ride.make_name_equals_any("Chevrolet", "Tahoe")

    Does that fix the issue?

  • omarvelous

    omarvelous July 31st, 2009 @ 12:16 AM

    Well it would have to be make_model_name, but I would have to have an association to make_model, which given it's a STI, isn't really easy to do... since... well, hopefully you can see why...

    Only way I'd get this to work is splitting the STI, which i can do... would mean more maintenance, but it would fix this issue...

    I was trying to do this using a "filter" method i created based off the order method you provide..

      def filter(search, options = {}, html_options = {})
          options[:params_scope] ||= :search
          options[:as] ||= options[:by].to_s.humanize
          options[:filter_scope] ||= "#{options[:by]}"
          new_scope = options[:filter_scope]
          link_to options[:as], url_for(options[:params_scope] => search.conditions.merge( { "#{options[:on]}_like" => new_scope } ) ), html_options

    Where it would work like your order, just append search conditions based on links that a user can select from a list

  • Ben Johnson

    Ben Johnson August 7th, 2009 @ 06:30 PM

    • State changed from “open” to “moved_to_github”

    [state:"moved_to_github" bulk edit command]

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