binarylogic / searchlogic

Searchlogic provides object based searching, common named scopes, and other useful tools.
http://rdoc.info/projects/binarylogic/searchlogic
MIT License
1.39k stars 133 forks source link

OR fails with associated classes #23

Closed twalpole closed 13 years ago

twalpole commented 15 years ago

Using OR searches with associated classes fails with Searchlogic::NamedScopes::OrConditions::UnknownConditionError errors

laserlemon commented 15 years ago

I also encountered this and just confirmed it as a bug. The README says that the OR-joined scopes should work with scopes on associations but that doesn't seem to be the case and the code comments make it seem as though it's still pending.

In my fork, I've written a failing test and a possible solution which passes the test.

http://github.com/laserlemon/searchlogic

swards commented 14 years ago

I also encountered this. The query created uses an inner join, where a left outer join would produce the desired results. In my case, the associations are not required, so the query returns no results.

konzeptual commented 14 years ago

This issue seams to be resolved in the very latest version ( I am using 2.4.7)

twalpole commented 14 years ago

Yes - the issue I entered this for was fixed quite a few versions back

twalpole commented 14 years ago

This can be closed

cknoxrun commented 14 years ago

This issue seems to have reappeared in 2.4.11. It is doing inner joins again when using ORs on associated scopes. Can anyone else confirm this?

mgomes commented 14 years ago

I am also experiencing this in 2.4.11

cknoxrun commented 14 years ago

I ended up creating a custom named_scope for this case, and it seems to work. I'm not sure if this is just how we were supposed to handle these joins all along? I was still having this issue in 2.4.7, so maybe it is more a problem with the documentation? Some clarifying thoughts from the authors/contributers would be helpful (thanks!). Anyways, this is what I did, it might help for others: named_scope :names_like, lambda { |n| { :conditions => [ "drugs.name LIKE ? OR brands.name LIKE ? OR drug_synonyms.synonym LIKE ?", "%#{n}%", "%#{n}%", "%#{n}%" ], :joins => left_outer_joins([ :brands, :synonyms ]), :select => "DISTINCT drugs.*" } }

In my form I then have: <%= form.text_field :names_like, :title => 'Search name, brands, and synonyms' %>

mgomes commented 14 years ago

I am trying to do something a little different. For example, say you have a parent model Building and it has_many :rooms.

If you attempt Room.building_meets_code_or_building_energy_efficient(1) Searchlogic will attempt to search for attributes in Building named meets_code_ and energy_efficient_. Note the extra underscore.

To get around this behavior I created corresponding named_scopes that contain the extra underscore.

noahduncan commented 13 years ago

@cknoxrun I have the same issue in 2.4.27, some detail on my setup follows :part (has description and extended_description_id fields) belongs_to :extended_description

:extended_description (has body field) has_one :part

Calling: Part.description_or_extended_description_body_like("heater") yields an inner join of Part.description and ExtendedDescription.body

nbarthelemy commented 13 years ago

I dealt with this issue in 2.4.27 and 2.5.4. The issue is line 108 in or_condition.rb. It need to be:

path << details[:association].name

not

path << details[:association]

ghost commented 13 years ago

I just released a fix, 2.5.5 fixes this. Thanks!