CARVIEW |
binarylogic / searchlogic
- Source
- Commits
- Network (78)
- Issues (77)
- Downloads (75)
- Wiki (1)
- Graphs
-
Branch:
master
click here to add a description
click here to add a homepage
-
Association chains that mention the same table twice produce incorrect results
6 comments Created 10 months ago by GFunk911For the example, take the following schema:
Product: id
ProductCategory: id, product_id, category_id
Category: idRows: You have one product in multiple categories
Product id: 1ProductCategory id: 1, category_id: 1, product_id: 1
ProductCategory id: 2, category_id: 2, product_id: 1Category id: 1
Category id: 2Say you want to find all categories that contain a product which is also in category X.
The following scope returns 2 copies of category 1, since the where sql references the wrong product_categories table. It should return categories 1 and 2
Category.product_categories_product_product_categories_category_id_equals(1)
SELECT
categories
.* FROMcategories
INNER JOINproduct_categories
ON product_categories.category_id = categories.id
INNER JOINproducts
ONproducts
.id =product_categories
.product_id
INNER JOINproduct_categories
product_categories_products ON product_categories_products.product_id = products.id
WHERE (product_categories.category_id = 235)
Comments
-
@Ben Johnson|S| newfrom-lighthouse
Hello, here is a feature request
Using named scope like "created_at_after" with helper like "form.date_select" which produces created_at(1i), created_at(2i) ... are not handled.
@@@ html <% form_for(@foos) do |f| %>
<p> <%= f.label :bar_at, ’Bar at after’ %> <%= f.date_select :bar_at_after %> </p> <p> <%#= f.label :bar_at, ’Bar at after’ %> <%#= f.text_field :bar_at %> </p> <p> <%= f.label :bar %> <%= f.text_field :bar_equals %> </p> <p> <%= f.submit :search %> </p>
@@@
It ends in an obvious attribute(xi) not recognized. I can still used a text_field helper and pass a string as shown in the example, but that’s not always what can be expected in a search form.
It would be nice feature to have.
JH. Chabran
This ticket has 0 attachment(s).
Comments
binarylogic Sun Aug 09 01:51:30 -0700 2009 | linkI agree, I tend to always use a javascript calendar date select, etc. So I have never had this issues. I don't think this would be too difficult to solve.
I added support for Date, as you can see there : https://github.com/jhchabran/searchlogic/blob/2d3d7ac5d7a267c8a367bf0066683a9212efc1de/lib/searchlogic/search.rb
It lacks support Time class and maybe some cosmetic changes, but it basically works. ( an expectation was added to search_spec too ).
binarylogic Sun Aug 09 20:54:50 -0700 2009 | linkThanks for doing this. I tried refactoring the code and couldn't really follow it. I can't safely pull that code in because I don't completely understand it and it was really hard to follow.
binarylogic Sun Aug 09 20:56:28 -0700 2009 | linkAlso, that code would be best in a completely separate module instead of changing the class itself.
Separating it in a module is basically doable, but it will leads to hook Search#conditions=, Search#method_missing, which seems to me to be a lot of work for just one line and maybe a bit unreliable if you modify these later.
This feature is clearly not critical, but it feels to me that not supporting all form helpers makes it at just a small step from being completely transparent for developers using it.
You wrote you found it hard to follow, can you detail a bit more ?
Rails multi-parameters are weird and in my opinion should be handled earlier, so params[:created_at] should return a date object or at least one string when submitted with three parameters, which would solve everything in the current case.
This forces me to rewrite handling code for theses parameters because as they did in AR it's coupled with models, and leads to a tricky regexp ( created_at(3i) means third argument with need to be casted as an integer), it's easy to write clear code for that, but in the original AR multi-parameter stuff, they allowed support for things like attribute(4f) and I kept support for these to stay coherent.
It was that part you found confusing or something else ?
And btw while I'm here thanks for SearchLogic, I saved much time using it :-)
binarylogic Tue Aug 18 22:40:17 -0700 2009 | linkI agree, the rails multi parameters are weird and complicated, which is why I try to avoid them. I don't know that your code was hard to follow, its the problem at hand that is hard. My BIG thing with v2 is to keep it simple, and if I add in a feature it needs to be separate out into its own module. v1 got bloated and very hard to maintain. I want to keep v2 simple. Simplicity is better for everyone. Less problems, better performance, etc.
So if you could split this out into a multi parameter module I would add that in. I added your code it and it started to get complicated and harder for me to maintain, which is why I removed it.
Thanks.
We needed this for a client project earlier today so I spent some time writing an alternative implementation. It hooks in a bit differently (alias_method_chain with conditions=) but it works well for the stuff I've tested.
It's a bit more complex than jhchabran's approach as well (although, it was
inspired / designed like it in some parts) and it's built around AR's implementation
/ how the AR implementation is coded as of Rails 2.3.You can see the patch changes at https://github.com/Sutto/searchlogic/, with the actual implementation itself at https://github.com/Sutto/searchlogic/blob/master/lib/searchlogic/search_ext/multiparameter_assignment.rb
-
conditionsgroupgrouping@Ben Johnson|S| openfrom-lighthouse
hey,
is something like a ’not_group’ available within searchlogic (or in combination with ActiveRecord)?
e.g.:
s=System.new_search
=> #25}>g=s.conditions.not_group => # g.name_equals="test" => "test" g.descr_equals="test" => "test" s.count => 95
generated sql:
SQL (0.5ms) SELECT count("systems".id) AS count_id FROM "systems" WHERE (NOT (("systems"."name" = ’test’ AND "systems"."descr" = ’test’)))This ticket has 0 attachment(s).
Comments
binarylogic Fri Aug 07 15:24:37 -0700 2009 | linknot_group support
After digging around, trying to find another solution without success I decided to implement that feature within searchlogic. The changes are committed to a fork of searchlogic (https://github.com/sdecastelberg/searchlogic/tree/master). Maybe someone can have a look at it. Feedback is appreciated. (i’m rather new to ruby/ror..)
The implemented not_group works fine for my needs.
by sdecastelberg
binarylogic Fri Aug 07 15:24:42 -0700 2009 | linknot_group support
Why wouldn’t you adjust your conditions to have the opposite effect instead of doing a not_group. Either way, whatever works for you, if you like the not_group solution then I think its fine, but couldn’t you accomplish the same thing by adjusting your conditions?
by Ben Johnson
binarylogic Fri Aug 07 15:24:45 -0700 2009 | linknot_group support
the not_group was initially needed for a monitoring/inventory application to map network components (software and hardware) and provide configuration for different network and application layer scanners.
For bulk editing, reporting, etc. we needed an exact match search for every single attribute on every model.
Search Dialog
*group1
*SnmpScan (include) *oid=ssCpuIdle *alarm-threshold=90 *System (exclude) *Status=Test *Modified Date start: 2009-01-01
would match the following search (when searching for systems)
(system.snmp_scan.oid=ssCpuIdle AND system.snmp_scan.alarm_threshold=90) AND NOT (system.state=Test AND modified_date>=2009-01-01)
The attributes are grouped model-wise and can be combined differently (and, or) and the search can be expanded by adding other groups with subgroups.
The include/exclude flag just sets the not_group attribute on the group, without having to negate all the single attributes.by sdecastelberg
binarylogic Fri Aug 07 15:24:47 -0700 2009 | linknot_group support
I see what you’re saying. But searchlogic deals with single conditions. If you are writing your own named scope for these conditions why not add in the "NOT" yourself? I guess I’m a little confused how you are using searchlogic and how you want to use it.
by Ben Johnson
-
associationsordersearch@Ben Johnson|S| openfrom-lighthouse
Consider a class Foo, with associated Bars:
@@@ ruby class Foo < ActiveRecord::Base
has_many :bars, :order => :name end
@@@If you do:
@@@ ruby Foo.bars.search(:order => "ascend_by_size").all
@@@The results are still sorted by :name -- i.e. the "ascend_by_size" :order argument to the #search method is ignored.
The SQL query looks like:
@@@ sql SELECT * FROM bars WHERE bars.foo_id = 1 ORDER BY bars.name;
@@@--
Is this a bug?
This ticket has 0 attachment(s).
Comments
binarylogic Fri Aug 07 15:25:14 -0700 2009 | link#search ignores :order argument on associations which have an order defined
Hmm, I’m not sure, let me add a failing test and play around with it. If you want fork the project and write a quick failing test. It looks like the Searchlogic::Search#current_scope is taking priority.
by Ben Johnson
Here is a failing spec: It's running through a defined named_scope, same issue though:
it "should overwrite ordering through a named_scope" do %w(bjohnson thunt).each { |username| User.create(:username => username) } User.class_eval { named_scope :by_username, :order => "username DESC" } User.by_username.ascend_by_username.should == User.all(:order => "username ASC") end
Hmm, looks like this is the same problem as https://github.com/binarylogic/searchlogic/issues#issue/17 which is an AR problem, not a searchlogic one
-
orderorder_by@Ben Johnson|S| openfrom-lighthouse
I would like to have ordering by multiple columns, where each of them can be ascending and descending. Example: "ORDER BY date ASC, id DESC"
What about creating scope like this:
order_by_date_asc_and_id_descSadly, nesting orders with scopes is not possible in Rails 2.3.2, see here:
https://rails.lighthouseapp.com/projects/8994/tickets/2253-named_scope-and-nested-order-clausesSo it should be created as ONE scope.
This ticket has 0 attachment(s).
Comments
binarylogic Fri Aug 07 15:26:14 -0700 2009 | linkMulti column ordering
Yeah, that would be a nice feature and it’s probably something AR scopes should handle anyways. It looks like it will get applied though, I think they are just waiting on better tests.
by Ben Johnson
binarylogic Fri Aug 07 15:26:16 -0700 2009 | linkMulti column ordering
Actually my tests are showing that AR does support multi column ordering by chaining named scopes. Are you not seeing that?
by Ben Johnson
binarylogic Fri Aug 07 15:26:20 -0700 2009 | linkMulti column ordering
Hm, I tested the following with Rails 2.3.2:
@@@ Contact.scoped(:order => ’name DESC’)
=> SELECT * FROM
contacts
ORDER BY contacts.name DESCContact.scoped(:order => ’name DESC’).scoped(:order => ’id DESC’)
=> SELECT * FROM
contacts
ORDER BY contacts.name DESC@@@
You will see that the second scope is ignored.
Are we talking about different things? ;-)
by Georg Ledermann
binarylogic Fri Aug 07 15:26:22 -0700 2009 | linkMulti column ordering
Yeah, my fault, it does chain them together when you pass order in an action method:
User.ascend_by_id.ascend_by_email.first(:order => "test") ActiveRecord::StatementInvalid: Mysql::Error: Unknown column ’test’ in ’order clause’: SELECT * FROM `users` ORDER BY test, users.id ASC LIMIT 1
But for me to make this work I would have to alter the default behavior of AR, which I don’t think is a good idea. I’m not sure what to do. I could do some tricky things in the Search object, but I want that to remain consistent with calling the named scopes directly.
by Ben Johnson
binarylogic Fri Aug 07 15:26:25 -0700 2009 | linkMulti column ordering
I agree with you. My current solution to have mutliple ordering is by adding some named scopes like this:
@@@ ruby named_scope :ordered_by_name, :order => ’last_name ASC, first_name ASC’Contact.search(:ordered_by_name => true)
@@@Somtimes I need to override the predefined searchlogic ordering scopes like this:
@@@ ruby # Preserve insertion order for sorting by date named_scope :descend_by_date, :order => ’date DESC, id DESC’ named_scope :ascend_by_date, :order => ’date ASC, id ASC’ @@@For me, it’s ok to stay with this. We will see if ActiveRecord will merge multiple ordering scopes in the future.
by Georg Ledermann
-
@Ben Johnson|S| openfrom-lighthouse
I tried long time to deal something with model translations tables, but i can’t figure out anything. Maybe somebody will find way to filter data by field from translations table...
This ticket has 0 attachment(s).
Comments
binarylogic Fri Aug 07 15:26:49 -0700 2009 | linkSome way to deal with i18n (*_translations tables)?
I’m not sure what you are asking.
by Ben Johnson
binarylogic Fri Aug 07 15:26:54 -0700 2009 | linkSome way to deal with i18n (*_translations tables)?
When using i18n with rails (in activerecord), translated columns are keept in other table (eg. you have table articles, so translated fields like title, content etc are in article_translations table). When i try search something in title of arcticle then i have error - unknown attribute. You have to do something like:
- check if table articles have specified attribute
- if have then process conditions for it
- if not then check if is article_translations table (or ArticleTranslation model will be easier)
- if exists then check attribute and process conditions
- if not then raise error
Im using for now such mechanism in my controller, but it’s not much pretty way...
by Kriss Kowalik
binarylogic Fri Aug 07 15:26:56 -0700 2009 | linkSome way to deal with i18n (*_translations tables)?
I see what you are saying. The translations / mapping should happen above board, I don’t feel like this is searchlogic responsibility. Does active record do this with their attributes when trying to create / update records?
by Ben Johnson
-
Order only check columns, why not checking additional values requested via SELECT
3 comments Created 9 months ago by ZenCocoonHi Ben,
Looks like the order option only accept valid columns names. What about additional values requested with SELECT.
Example :
SELECT
users
.*, COUNT(1) as count FROM ... ORDER BY count ASCIn this case, ordering by count can be extremely useful but doesn't look supported right now.
As quick workaround creating the named "scope ascend_by_count" and "descend_by_count" makes the job but still overload your models far to quick while adding such feature to searchlogic would keep things light and clean.
Thanks again for this great lib and excellent rewrite,
Warm regards,
Sebastien Grosjean - ZenCocoon
Comments
Yeah, I like this.
I often want to order asc/desc using the count of an associated class.
When you use a counter_cache this is pretty easy to do using the counter_cache column but I don't want to use counter_caches everywhere and every time.
Nicolas.
binarylogic Fri Sep 11 22:32:06 -0700 2009 | linkHi Sebastien, I'm not sure I follow, how would you dynamically create that scope? I guess you cold make an exception for count if that is an AR default.
Hi Ben,
Didn't look the inside of named scope system yet (I actually should). The fact is that as of now, it seems that you allow as order, only existing column names from the tables queried.
I my case, I'm looking to also allow values defined by the SELECT parameter. It can be "count" as shown in the previous example but also different values depending your named scope
Example :
named_scope :select_with_author_name_as_writter,
:select => "posts.*, authors.name AS writter", :joins => "LEFT OUTER JOIN authors ON authors.id = posts.author_id"
Post.select_with_author_name_as_writter.descend_by_writter NoMethodError: undefined method `descend_by_writter' for #<Class:0x101d8dc48>
...Please let me know if it's still blurred.
-
Using OR searches with associated classes fails with Searchlogic::NamedScopes::OrConditions::UnknownConditionError errors
Comments
laserlemon Thu Oct 01 11:45:50 -0700 2009 | linkI 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.
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.
This issue seams to be resolved in the very latest version ( I am using 2.4.7)
Yes - the issue I entered this for was fixed quite a few versions back
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?
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' %>
I am trying to do something a little different. For example, say you have a parent model
Building
and ithas_many :rooms
.If you attempt
Room.building_meets_code_or_building_energy_efficient(1)
Searchlogic will attempt to search for attributes in Building namedmeets_code_
andenergy_efficient_
. Note the extra underscore.To get around this behavior I created corresponding named_scopes that contain the extra underscore.
-
It seems its not working from the form:
- form_for @search do |f| %p = f.text_field :name_or_description_like = f.submit "Search"
It works from the console, using Report.name_or_description_like but when I use it in the form I just get a "false" result in the text field, and no filters are applied (I checked the logs).
Let me know if you need any more info (I'm using the latest version of searchlogic and rails 2.3.2).
I also run a test creating a new rails app from scratch with rails 2.3.3 and exactly the same happens, it doesn't get filtered and it shows a "false" string in the form (as a response).
Thanks a lot for a great plugin!
Comments
carlosantoniodasilva Thu Sep 03 04:53:28 -0700 2009 | linkThe same for me, doing this works:
Update.subject_or_body_contains "list"
But this does not:
Update.search(:subject_or_body_contains => "list").all
Rails 2.3.4, SL 2.3.3:
Update.search(:subject_or_body_contains => "list") produces:#<Searchlogic::Search:0xb7457ac4 @klass=Update(id: integer, subject: string, body: string, created_at: datetime, updated_at: datetime), @conditions={:subject_or_body_contains=>false}, current_scopenil
joerichsen Fri Sep 11 00:15:58 -0700 2009 | linkSame for me Rails 2.3.4 and SL 2.3.3
User.name_or_email_like('Gade').size => 2
User.search(:name_or_email_like => 'Gade').all.size => 379?
There will be exacly the some with:
s = User.search
s.name_or_email_like('Gade')
That's because in Search model in "or" conditions is some bug that set any "or" condition to false(it is visible as I showed earlier but value = false) so no filtering by that condition. Right now I can't find the exact line, but I think that it is somehow connected to validating scope "or" spliting (lib/searchlogic/named_scopes/or_conditions.rb)
Any ideas?Confirming:
>> User.last_name_like_or_first_name_like('tom').size => 1 >> User.search(:last_name_like_or_first_name_like => 'tom').size => 3
Same for me:
>> User.login_or_email_like('lec').size SQL (0.4ms) SELECT count(*) AS count_all FROM `users` WHERE ((users.email LIKE '%lec%') OR (users.login LIKE '%lec%')) => 2 >> User.search(:login_or_email_like => "lec").size SQL (0.3ms) SELECT count(*) AS count_all FROM `users` => 23
gavinhughes Thu Sep 17 00:31:48 -0700 2009 | linkI'm having the same problem.
Ok… I encountered the same problem and checked the source code (quite impressive). After some digging and testing I found the problem, but so far I did not came up with a perfect solution for it. Until then, I have a partial quick fix which I provide at: https://github.com/namxam/searchlogic/
This fix is only working for fully qualified conditions:- name_and_username_like => not working
- name_like_and_user_name_like => working
I hope I can provide a fix for the remaining problem as well
Hi! I finally fixed this problem even for not fully qualified conditions. And also added type convertion, so User.search(:id_or_age_lte, '10') will now work properly.
The fix is here: https://github.com/nebolsin/searchlogic/
That's great news. I hope it will be pulled into the master repository soon!
I am running 2.3.5, but it still appears to be present in this version as well. I am new to searchlogic, so if a work around has been mentioned - I apologize, and point me in the right direction.
note: I did see nebolsins repo above, but have not tried it yet.
First off , thanks for the awesome search dsl !
Is there any chance of the fix in https://github.com/nebolsin/searchlogic/ being pulled into the official repo ? I keep running into this problem and do not want to create yet another fork for this one issue.
thanks .
Awesome job on searchlogic, but I agree, this one is bugging me too!
-
I'm looking for a way to use Searchlogic to find records which DON'T HAVE records for a given association. For example: "Find all Contacts which have no addresses"
class Contact < ActiveRecord::Base has_many :addresses end class Address < ActiveRecord::Base belongs_to :contact end
In SQL, this query does the job:
SELECT contacts.* FROM contacts LEFT JOIN addresses ON (addresses.contact_id = contacts.id) WHERE (addresses.ID IS NULL)
So far as I understand, Searchlogic does not support this. Of course, I can creare a named scope for this:
class Contact named_scope :without_addresses, :joins => 'LEFT JOIN addresses ON addresses.contact_id = contacts.id', :conditions => 'addresses.id IS NULL' end
But I would like to have a more dynamic way, means Searchlogic creating this scope on the fly. What do you think?
Comments
binarylogic Fri Aug 28 07:04:55 -0700 2009 | linkSearchlogic has a method called left_outer_joins that will create the join for you. But to the best way to solve this is to just add a named scope for each association. I would probably just create another module and do it in there.
Yes, creating scopes the explicit way will work. But I'm looking for a dynamic way. One more example:
Right now, with Searchlogic it's possible to find all contacts with an address in Germany:
>> Contact.search(:addresses_country_equals => 'DE') => "SELECT `contacts`.* FROM `contacts` INNER JOIN `addresses` ON addresses.contact_id = contacts.id WHERE (addresses.country = 'DE') "
This creates an INNER JOIN, works fine.
Now, I want to find contacts which don't have an address in Germany. It would be great if Searchlogic could do this, perhaps with this statement:
>> Contact.search(:without_addresses_country_equals => 'DE') => "SELECT `contacts`.* FROM `contacts` LEFT JOIN `addresses` ON (addresses.contact_id = contacts.id AND addresses.country = 'DE') WHERE (addresses.id IS NULL) "
The idea is: If the hash key has a "without" in front of the association name, Searchlogic builds the query with the LEFT-JOIN/ID-IS-NULL-Pattern.
IMHO such a feature (implemented in Searchlogic) would be very powerful.
One addition: The whole thing with the left join is required to include contacts which does not have any address, too. You may think my example can be reached with this simple query:
Contact.search(:addresses_country_is_not => 'DE')
But this gives a different result set, because it does not include the contacts which have no addresses at all.
-
stack level too deep in merge_scopes_with_or
2 comments Created 9 months ago by dougalcornClass Message < ActiveRecord::Base has_many :addresses, :class_name => "MessageAddress" named_scope :system, :joins => 'LEFT JOIN message_addresses ON message_addresses.message_id = messages.id', :conditions => 'message_addresses.id IS NULL' end Class MessageAddress < ActiveRecord::Base belongs_to :message belongs_to :recipeint, :polymorphic => true named_scope :for_user, lambda { |user| { :conditions => ["blah blah blah", user] } } end
Now when I do Message.addresses_for_user_or_system(user) It generates a stack level too deep.
searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:104:in `send' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:104:in `merge_scopes_with_or' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:104:in `collect' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:104:in `merge_scopes_with_or' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:99:in `create_or_condition' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:104:in `send' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:104:in `merge_scopes_with_or' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:104:in `collect' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:104:in `merge_scopes_with_or' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:99:in `create_or_condition' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:22:in `addresses_for_user_or_without_addresses' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:22:in `send' searchlogic (2.3.1) [v] lib/searchlogic/named_scopes/or_conditions.rb:22:in `method_missing' vendor/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:in `method_missing' app/models/message.rb:19:in `for_user'
There's a couple problems here. Obviously there's a bug that causes the stack level too deep. But the or using an associated named scope seems to be completely unsupported. So maybe my answer is to just not do that. Oh, and I manually patched my searchlogic 2.3.1 with the latest version of or_condition.rb
Comments
binarylogic Fri Sep 11 22:25:13 -0700 2009 | linkYeah, association support is coming, I'm working out a couple of issues. I'll throw this in the test suite.
dougalcorn Sat Sep 12 06:13:11 -0700 2009 | linkSearchlogic really is great. We're making a lot of use out of it. In this case we ended up just writing our own named scope: break out the sql!
-
Search with empty array returns all results instead of none
1 comment Created 8 months ago by gravisNot sure I understand correctly, it might not be a bug.
If I use :Activity.user_id_equals_any(current_user.friend_ids)
I expect this to be equivalent to
Activity.all(:conditions => {:user_id => current_user.friend_ids})and it's not. The first expression will return all activities if the user doesn't have friends, and the second no activity, as expected.
thanks !
Comments
-
I guess the example below will speak on it's own :
named_scope :select_with_authors,
:joins => "LEFT OUTER JOIN authors ON authors.id = posts.author_id"
Post.select_with_authors.descend_by_author_name ActiveRecord::StatementInvalid: Mysql::Error: Not unique table/alias: 'authors': SELECT
posts
.* FROMposts
INNER JOINauthors
ONauthors
.id =posts
.author_id LEFT OUTER JOIN authors ON authors.id = posts.author_id ORDER BY authors.name DESCPlease let me know if you like further details.
Comments
pschrammel Tue May 04 02:32:20 -0700 2010 | linkthis happens with :include in the named_scope, too.
-
Could be possible to add OR support even to Search class. I would like do this
Book.search(:title_or_description_like => 'ruby')
or in chain
s = Book.search s.title_or_description_like('ruby')
Now it produce only
s.conditions => {:title_or_description_like => false}
Thanks for wonderful work.
Comments
is this going to be worked on, I just ran into this. A large part of using search logic is the search method, but you currently cant use or statements like: User.search(:gpa_lte_or_null => 4) It just ignores all the conditions.
theworkinggroup Tue Oct 27 13:40:12 -0700 2009 | linknoticed the same issue. a bit annoying.
would be very helpful if the search object supports OR chaining as well.
kylewelsby Thu May 06 09:32:55 -0700 2010 | linkI have an issues somewhat like this,
Trying to search an association if it exists.Place.{:title, :brand_id}
Brand.{:title}Place.title_or_brand_title_like_any(q)
only searches places that are associated with a brand, fails to return places without a association.
-
Please replace line 99 of search.rb
scope = conditions.inject(klass.scoped(current_scope)) do |scope, condition| with
scope = conditions.inject(klass.scoped(current_scope || {})) do |scope, condition| otherwise it doesn't work with Rails 2.2.2This issue was already described here https://binarylogic.lighthouseapp.com/projects/16601/tickets/94-nil-object-error but no solution has been given apart from upgrading to Rails 2.3.x, which sadly is often not possible.
Comments
Please log in to comment. -
Typecasting Range values isn't working and although Ranges aren't typically passed into the arguments for the search method, it's certainly not impossible (as I found out). The individual scopes work correctly, but when accessed through the
search
method, they fall apart. I've forked the repo, added a failing test and presented a solution.Comments
Please log in to comment. -
An existing named scope that shares its name with one of the model's columns is ignored when accessed via the
search
method. Thenormalize_scope_name
method appends "_equals" with no regard for whether the scope already exists. I've forked the repo, added a failing test and presented a solution.Comments
Please log in to comment. -
Multiple record copies returned for habtm association
4 comments Created 8 months ago by infraredI committed a test case that reproduces the problem in https://github.com/infrared/searchlogic/commit/34d3b89122812df2b4fa9a0c8339674ecf25f206
When you have two models joined with has_and_belongs_to_many and you search one of them with something like:
FirstModel.search(:second_models_id_equals_any => [1, 2]).all
multiple copies of the same FirstModel record will be returned if a the record happens to be associated with more than one SecondModels.
Comments
laserlemon Tue Dec 08 09:39:49 -0800 2009 | linkSame issue here. Definitely not the expected behavior.
I have the same issue, using a plain has_many association.
I've been fighting this issue for a while... is there a work around?
I've worked around this by writing a custom named scope. Using my original example, I could write something like this:
class FirstModel named_scope :with_second_models, lambda {|ids| matches = ids.map{ "second_model_id = ?" }.join(" OR ") {:conditions => "first_models.id IN (SELECT first_model_id FROM the_join_table WHERE #{matches})"} } end
After that it can be used like this:
FirstModel.search(:with_second_models => [1, 2]).all
-
For some reason, when I try to do an is(#) or equals(#) or _eq(#), I always get an error about there being the wrong number of arguments.
Table:
Workflow(id: integer, created_by: integer, held_by: integer, prefix: integer, project_id: integer, form_id: integer, fields: text, subject: string, priority: integer, status: integer, due_date: datetime, created_at: datetime, updated_at: datetime)
Records:
+----+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ | id | cre... | hel... | prefix | pro... | for... | fields | sub... | pri... | status | due... | cre... | upd... | +----+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ | 4 | 1 | 1 | 0 | 3 | 9 | 28d... | | 2 | 0 | | 200... | 200... | | 5 | 1 | 1 | 0 | 2 | 7 | | | 2 | 0 | | 200... | 200... | +----+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
Trying to execute Workflow.status_is(0) results in:
ArgumentError: wrong number of arguments (2 for 1) from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/conditions.rb:93:in `attribut e_condition' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/conditions.rb:93:in `create_p rimary_condition' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/conditions.rb:151:in `call' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/conditions.rb:151:in `scope_o ptions' from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/named_scope.rb:91:in `call' from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/named_scope.rb:91:in `named_scope' from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/named_scope.rb:96:in `call' from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/named_scope.rb:96:in `status_equals' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/conditions.rb:175:in `send' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/conditions.rb:175:in `create_ alias_condition' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/conditions.rb:83:in `create_c ondition' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/conditions.rb:66:in `method_m issing' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/association_conditions.rb:19: in `method_missing' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/association_ordering.rb:27:in `method_missing' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/ordering.rb:30:in `method_mis sing' from C:/Ruby/lib/ruby/gems/1.8/gems/searchlogic-2.3.5/lib/searchlogic/named_scopes/or_conditions.rb:24:in `metho d_missing' from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1833:in `method_missing' from (irb):11
Comments
laserlemon Tue Oct 06 08:20:19 -0700 2009 | linkIt looks like this may be my fault. The problem is that
ActiveRecord::Base.attribute_condition
only started accepting two arguments when it started supporting end-exclusive ranges, starting at ActiveRecord 2.3.1. I see you're using 2.2.2, which is causing the error.Is it worth having a version check in there for backwards compatibility?
laserlemon Tue Oct 06 08:32:37 -0700 2009 | linkCould be. I defer to Ben.
Ok, I updated my RAILS_GEM_VERSION from 2.2.2 to 2.3.4 (not sure why I never changed the default :P) and can confirm that it works properly.
xdotcommer Fri Mar 05 12:07:50 -0800 2010 | linkI've fixed this in my own branch:
https://github.com/xdotcommer/searchlogic -
Search through association with column named "name" fails
1 comment Created 7 months ago by naven87Take the following models
Earnings: belongs_to :period
Period: has_many :earnings
if Period has a string column titled "name" the following search fails
Earnings.period_name_like("Q309") with a MySQL error that the Table/Alias periods is not unique. If I look at the generated MySQL, it creates both an inner and and outer join on the periods table against the earnings table.If I instead call Earning.period_id_is(7) it works fine and no error is generated. The MySQL query only includes one outer join and no inner join.
Comments
So after more digging, I found that the column name was irrelevant, the real issue was any default scope on the model is merged into the query without concern for overlap. So if the default scope has a join or include in it, that will be included directly in the final query and if the searchlogic portion uses the same table name it will add it's own join into the query which can create the problem described
-
I am trying to use jqGrid's search function with searchlogic. The rub of it is jqGrid's aliases for 'less than or equal to' and a few others do not map onto searchlogic's. Right now, I am creating a hash constant in the application controller to the mapping. What I would love is to have a configuration file that would allow me to extend the aliases. This way, I could drop that config file in any app I so chose that I wanted to use both libraries in concert. Anyway, thank you for such a wonderful plugin and if you would like me to write up a spec, I would be more than happy to.
cheers,
-xnComments
Please log in to comment. -
Ben seems to show examples of "User.age_greater_than(20)" but age as an attribute is often used in models as a virtual attribute which calculates the current age from DOB.
When I use age as a virtual attribute and search on it, I get a "undefined method `age_greater_than' for #<Class:0x48fff80>"
What am I missing?
Comments
Searchlogic only generates ActiveRecord conditions as named scopes. Since your RDBMS does not know about the virtual attributes, this is impossible. Create your own named_scope, if neccessary, where you create the condition required for this to work.
-
searchlogic uses NULL value on search by date (created_at_gte)
6 comments Created 7 months ago by spovichDoing a simple search on the created_at column will produce SQL that has a NULL value for the date.
In the form, I input in ISO date format (YYYY-MM-DD), and the values shows in the params in the logs:
Parameters: {"commit"=>"Search", "action"=>"index", "controller"=>"foos", "search"=>{"city_like"=>"", "created_at_gte"=>"2009-04-13"}}
But the resulting SQL gives a NULL for the date:
SELECT * FROM "foos" WHERE (foos.created_at >= NULL)
Comments
Ok, the issue is that searchlogic silently makes a nil when you provide a date for a datetime field. However, ruby converts date to time without a problem:
Time.parse "Jan 1, 2009" => Thu Jan 01 00:00:00 -0800 2009
So, this seems to be a bug to me. I'll write some failing specs if you agree that it should be able to convert a date to a datetime when searching.
Taking another look and I see search_spec.rb line 267 is a search of a datetime that is given an date, so we agree that a Date should be upcast to a dateTime.
The specs all pass for me using the SQLite db, but trying the same thing with a stock rails app 2.3.4 and ruby 1.8.7 in IRB (console) with Postgres 8.3 and latest pg gem (0.8.0) will fail (running on OS X):
>> s = Message.search
=> #<Searchlogic::Search:0x35e6ad4 @klass=Message(id: integer, type: string, uuid: string, title: string, description: text, created_at: datetime, updated_at: datetime), @current_scope=nil>
>> s.created_at_after = 'Jan 1, 2009' => "Jan 1, 2009" >> s.created_at_after => nil
I don't know how to make this fail in SQLIte, but it is clearly failing in postgres. Any ideas?
Thanks,
JohnOk, so I modified the searchlogic test spec's to use postgres, and everything passes except the tests where LIKE becomes ILIKE. Must be something in our app, so will need to dig in further.
hopefully you are not using validates_date_time plugin which cast a datetime field to nil, I nuked the plugin from my project, all is working fine! yunks!
Yea, I eventually found that was the problem also. We switched to validates_timeliness which solved it. Looking at the code for validates_date_time reinforces the point that you should always review the code in plugins (some ugly code in there). If I had looked at it first, I would never have used it!
Thanks and sorry that I didn't follow up here with my findings before (maybe saved you some trouble).
-
This is what my search form sends:
"search"=>{"value_equals"=>"0..15000", "property_type_id"=>"1"}
The value_equals parameter is not read as a proper range, like from 0 to 15000 and while it should display as a result a record with value "10000", none is shown.
Comments
Please log in to comment. -
given a model
Company with attribute
nameand a model
Contact with attributes
first_name and last_nameThe Models are associated like this:
Contact belongs_to Company Company has_many ContactsI wish to make a search field into which I can type a portion of either the contact's first name, the contact's last name or the company name.
This works:
@contacts = Contact.company_name_like(params[:search])This works:
@contacts = Contact.first_name_or_last_name_like(params[:search])This does NOT work:
@contacts = Contact.company_name_or_first_name_or_last_name_like(params[:search]) It returns this error:
"The condition 'company_name' is not a valid condition, we could not find any scopes that match this."Evidently the scope company_name works by itself but not in combination with the other two. And the other two work, but not with company_name. I can only assume this has to do with the association and I am missing something obvious. Can anyone point out how to do it correctly?
The last example (company_name_or_first_name_or_last_name_like) worked in an earlier version of my app, when company was an attribute of Contact. When I made Company a separate model it stopped working.
I believe I have the current version of binarylogic-searchlogic. Rails version is 2.3.2, in development mode with standard sqlite database.
Thank you.
Comments
kylewelsby Thu May 06 09:46:17 -0700 2010 | linkI have the same issue, but no solution yet.
-
Only works when config.cache_classes = false
2 comments Created 7 months ago by jnarowskiI love search logic, but cannot get it to run in production mode. I found out that the cache_classes setting was the culprit. I know searchlogic works dynamically, but is there a way to enable searchlogic with class_caching?
When the classes are not cached I get:
undefined methodcached_tags_contains' for #<Array:0x4e064c0> or undefined method
id_contains' for #<Array:0x4e064c0>Comments
Has anyone found a workaround for this? I understand it is probably a bug in rails 2.3.5, but is there anyway to workaround the issue, other than defining a bunch of named_scopes manually or setting cache_classes to false?
I noticed this problem only when searchlogic is used to scope has_many associations. Base models don't cause this issue.
-
For some reason Searchlogic::Search.respond_to?(:paginate) returns false, even though you can call paginate on it.
This was messing up Hobo, so I monkey patched it:
class Searchlogic::Search
def respond_to?(symbol) return true if symbol === :paginate old_respond_to? symbol end endComments
Please log in to comment. -
The change log states that modifiers were added quite a while ago. However, I can't seem to use them. I keep getting errors like: "The lower_of_first_name_eq is not a valid condition. You may only use conditions that map to a named scope"
Are modifiers available in the latest release?
Comments
Please log in to comment. -
if Car has_many Tires
Tire has an integer column called sizeI should be able to do :
Car.tires_size_equals([4,5,6])
That fails though. I am able to do a single item though like Car.tires_size_equals(4)
Here is the error:
/Library/Ruby/Gems/1.8/gems/searchlogic-2.3.6/lib/searchlogic/named_scopes/conditions.rb:111: warning: multiple values for a block parameter (2 for 1)from /Library/Ruby/Gems/1.8/gems/searchlogic-2.3.6/lib/searchlogic/named_scopes/conditions.rb:173
ActiveRecord::PreparedStatementInvalid: wrong number of bind variables (2 for 1) in: support_regions.region_id IN (?)
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2398:in `raise_if_bind_arity_mismatch' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2350:in `replace_bind_variables' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2341:in `sanitize_sql_array' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2230:in `sanitize_sql' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1494:in `merge_conditions' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1492:in `each' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1492:in `merge_conditions' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1804:in `add_conditions!' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1687:in `construct_finder_sql' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1548:in `find_every' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:615:in `find' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:181:in `send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:181:in `method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2143:in `with_scope' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:113:in `__send__' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:113:in `with_scope' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:174:in `method_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:188:in `load_found' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:166:in `proxy_found' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:109:in `inspect' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:302:in `output_value' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:151:in `eval_input' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:263:in `signal_status' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:147:in `eval_input' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:146:in `eval_input' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:70:in `start' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:69:in `catch' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:69:in `start'
Comments
fearoffish Thu Feb 04 06:40:11 -0800 2010 | linkI get this on 2.4.7 still, fyi.
-
I found some old issues, concerning the date_select helper. It looks like there should be support to use this helper in combination with searchlogic.
But when I use <%= f.date_select :start_after, :include_blank => true %> it fails with
"The is not a valid condition. You may only use conditions that map to a named scope"Comments
philrosenstein Mon Dec 07 08:10:49 -0800 2009 | linkI have also encountered this issue. It looks like the dynamic finder is looking to match the individual date parts rather than the whole date...
undefined method `created_at_after(1i)='same probleme here :
The is not a valid condition. You may only use conditions that map to a named scope
I have encountered this issue as well. This is really a fundemental requirement in an application I am currently writing. I am a big fan of Searchlogic because it helps keep my code very clean and I'd hate to need to use something else in order to solve this problem.
I have write a quick/dirty solution to handle date_select :
make a file /config/initializer/searchlogic.rbHere's another version that also handles datetime_select:
Very good patch, you can fork the source and ask for a merge? I temporary just unpack the gem and patch it myself there.
philrosenstein Tue Apr 20 12:10:02 -0700 2010 | linkI forked and made the change in the gist. Seems to work well, but my pull request has gone unanswered https://github.com/philrosenstein/searchlogic
I also have a fork with this change : https://github.com/rubysolo/searchlogic
-
Having a named_scope :order (for the obvious reason of ordering) in a model, searchlogic ordering won't work anymore.
class Blog < ActiveRecord::Base
named_scope :order, lambda {|order| {:order => order}} endBlog.searchlogic(:order => "ascend_by_id") results in:
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'ascend_by_id' in 'order clause': SELECT * FROMblogs
ORDER BY ascend_by_id DESC LIMIT 1must have something to do with the def scope? method in search.rb
cheers
Comments
Please log in to comment. -
Hello
As searchlogic is able to read scopes, it would be great we could code User.search(:username_or_first_name_like => "ben") just like we use User.username_or_first_name_like("ben")
My 2 (euro)cents
Comments
Please log in to comment. -
Example for combine scopes with ‘OR’ is incorrect
2 comments Created 6 months ago by vandrijevikThe second example in the Readme under the "Combine scopes with ‘OR’" section reads:
User.id_or_age_lt_or_username_or_first_name_begins_with(10) => "id < 10 OR age < 10 OR username LIKE 'ben%' OR first_name like'ben%'"
however, running
User.id_or_age_lt_or_username_or_first_name_begins_with(10)
results in the SQL:"id < 10 OR age < 10 OR username LIKE '10%' OR first_name LIKE '10%'"
being run. Trying something like:
User.id_or_age_lt_or_username_or_first_name_begins_with(10, "ben")
raises a ActiveRecord::PreparedStatementInvalid, so there is no way to get the users whose id or age is less than 10 or username or first name begins with "ben".
It seems that the only scopes this will work for are ones that take the same number of arguments, and operate on the same type of field (since username like '10%' makes little sense).
Comments
I would love to have this ability. I can see two ways to implement the API, either as shown in the example
User.id_equal_or_name_equals(4,'ben')
or by making it possible to switch from 'AND' to 'OR' when chaining by appending 'or_':
User.id_equals(4).or_name_equals('ben')
I would prefer the latter approach, as it isn't ambiguous in cases where multiple arguments are being matched against:
User.id_equals_or_age_equals(16,17)
could be interpreted as id=16 || age=17 or as id=[16||17] || age=[16||17]
I would prefer the latter aswell, it'll be also kind of searchlogic1-like. any news on that issue?
-
Passing an array to like_any changes the search field value
0 comments Created 6 months ago by wksmallI've got a form like this:
<% form_for @search do |f| %>
<fieldset> <legend>Search</legend> <%= f.label(:source_like_any, "Source:") %> <%= f.text_field(:source_like_any, :size => '40') %> <%= f.submit("Search") %> </fieldset>
<% end %>
My controller does this:
def index
options = params[:search] options['source_like_any'] = options['source_like_any'].to_s.split @search = MediaItem.searchlogic(options) @results = @search.all.paginate(:page => params[:page], :per_page => 20)
end
I did this after watching Railscast #176 because I want to find all the words in any order.
The problem is that when the result form is displayed, the spaces are gone from the words in the search field. Resubmitting the search will get different results.
Any way around this or did I just miss something in the docs or Railscast?
Comments
Please log in to comment.
- @Ben Johnson▾
- associations▾
- conditions▾
- from-lighthouse▾
- group▾
- grouping▾
- named_scope▾
- order▾
- order_by▾
- search▾
- |S| new▾
- |S| open▾
- Apply to Selection
-
Change Color…
Previewpreview
- Rename…
- Delete




Yeah, I see what you're doing, AR supports this, I just need to figure out how to tap into it.
I also have something similar:
Will give:
SELECT
directions
.* FROMdirections
INNER JOINpoints
ONpoints
.id =directions
.end_point_id INNER JOINpoints
start_points_directions ONstart_points_directions
.id =directions
.start_point_id WHERE ((points.name = 'A') AND (points.name = 'B'))I'm facing the same problem.
I'm having a similar (or the same) problem, but with a single self-relationship in a table:
Create a project
rails searchlogic_test
cd searchlogic_test
script/generate model client name:string agency_id:integer
Add to db/seeds.rb
Client.create(:name => "Daniel (has no agency)")
the_agency = Client.create(:name => "TheAgency (is an agency)")
Client.create(:name => "Roberto (has agency)", :agency_id => the_agency.id)
app/models/client.rb
class Client < ActiveRecord::Base
belongs_to :agency, :class_name => 'Client', :foreign_key => :agency_id end
Setup the database
rake db:migrate
rake db:seed
Add to config/environment.rb
config.gem "searchlogic"
Test on console
script/console
c = Client.agency_name_like("the")
You'll see it runs the following SQL
SELECT "clients".* FROM "clients" INNER JOIN "clients" agencies_clients ON "agencies_clients".id = "clients".agency_id WHERE (clients.name LIKE '%the%')
But I think it should be
SELECT "clients".* FROM "clients" INNER JOIN "clients" agencies_clients ON "agencies_clients".id = "clients".agency_id WHERE (agencies_clients.name LIKE '%the%')
There were serious changes in Rails 3 that addresses this; Arel was introduced: "New Active Record chainable query language built on top of relational algebra". See https://guides.rails.info/3_0_release_notes.html
Same issue here.