binarylogic / searchlogic
- Source
- Commits
- Network (63)
- Issues (42)
- Downloads (48)
- Wiki (1)
- Graphs
-
Branch:
master
-
User.id_ne(10).login_not_like("root").count => 2644 User.id_ne(10).login_not_like("root").count => 2643
this produces
SQL (1.7ms) SELECT count() AS count_all FROM
usersWHERE (users.login LIKE '%root%') SQL (3.9ms) SELECT count() AS count_all FROMusersWHERE ((users.login LIKE '%root%') AND (users.id != 10))Comments
-
Just as ActiveSupport adds the blank? and present? methods to Object to detect if something is either nil or a blank string/array/whatever, I thought it might be nice to provide the blank and not_blank conditions in searchlogic.
Example:
Person.first_name_blank # => SELECT * FROM people WHERE first_name != '' OR first_name IS NULLJust something we've found to be convenient for our app. Patch w/ specs can be found here:
Comments
Commited to bedecfc, but I think it's not correct:
not_blank should be
column != "" AND column IS NOT NULLYou see, AND instead of OR. Otherwise, the result set includes empty strings.
binarylogic
Mon Aug 24 14:03:57 -0700 2009
| link
Cool, this has been added
You're quite right about that. I rewrote the spec for non_blank to be a little bit more clear. Here's the new patch:
Granted, I think that just saying column != '' will have the same results, since I believe NULL values won't even be considered in a string comparison. I left it as you suggested though for completeness' sake.
Hm, this issue show be re-opened until Ben has commited the patch...
binarylogic
Tue Aug 25 01:40:00 -0700 2009
| link
Thanks, this has been fixed.
-
It would be nice if one could do a simple
ORcondition like this.Article.name_or_content_like("something")I imagine the method_missing call could do a check for "
_or_" in the name, split by it, and add a condition for each resulting attribute.Comments
binarylogic
Tue Aug 18 21:51:19 -0700 2009
| link
Yeah, that's definitely doable. I'll throw something together for that.
josespinal
Tue Aug 18 22:27:27 -0700 2009
| link
Thanks!
binarylogic
Sat Aug 22 16:47:03 -0700 2009
| link
This has been added. It was actually pretty fun to code. Anyways, checkout the readme for some examples, or checkout the specs:
Let me know if you have any issues.
This works for me in a simple case like
Address.first_or_last_like("something")but im having problems with associations:
#Customer => has_many addresses Customer.address_first_or_address_last_like("something") # => error: The condition 'address_first' is not a valid condition, we could not find any scopes that match this. Customer.address_first_like_or_address_last_like("something") # => error: The condition 'address_last_like' is not a valid condition, we could not find any scopes that match thisAlso (im not sure why i was trying this) but this gave me a stack level too deep:
Customer.address_first_like_or_id_equals(0) -
ERROR MESSAGE: "ActiveRecord::StatementInvalid (PGError: ERROR: table name "tender_flags" specified more than once"
I am using a join with one table and as I use order by with that table it gives this error
Also any association based sorting by default use INNER JOIN but in my scenario I require LEFT JOIN any suggestion for getting through that issue. thanks.Comments
Details;
User.ascend_by_role_is_admin
in that case the query is;
User Load (1.5ms) SELECT "users".* FROM "users" INNER JOIN "roles" ON roles.user_id = users.id ORDER BY roles.is_admin ASCwhereas I need LEFT JOIN in the query for getting all records but order by is_admin field.
binarylogic
Tue Aug 18 22:43:53 -0700 2009
| link
I have this same problem in my application. There is no easy way around this. Your best bet is to write your own order condition that uses the same join so that AR will remove the duplicates. The other option is to give your left outer joins aliases like "optional_roles". This is what I've been doing in my apps. This really isn't an issue with SL directly, its more of an issue with just combining named scopes. If you want to modify the join that SL creates, you are better off just creating the named scope yourself and adding the join that you want.
Thanks.
-
2 comments Created 5 months ago by binarylogic@Ben Johnsonxfrom-lighthousexorder() doesn’t order correctly from searchlogic > 2.1.5|S| newxPlease find attached test app to demonstrate the strangely order/sorting behaviour with searchlogic > 2.1.5
This ticket has 1 attachment(s).
Comments
binarylogic
Tue Aug 18 22:37:08 -0700 2009
| link
Can you please create a failing test for this. It would be much easier. I couldn't reproduce any problems with the app. Thank you.
-
5 comments Created 5 months ago by binarylogic@Ben Johnsonxfrom-lighthousexSorting on a single table Help|S| openxI tried out using the example code on my controller i.e @search = new_search(params[:search]) and I am able to sort and view pages in 10, 25 etc.. However, when I add conditions the data is displayed correctly but the sorting and page links don’t work. I am trying this on a single table of data. Your comments and advised would be helpful.
This ticket has 0 attachment(s).
Comments
binarylogic
Fri Aug 07 15:24:05 -0700 2009
| link
Sorting on a single table Help
I have no idea. Can you please create a failing test, and then I can fix the issue. By test, I mean a test I can run in the searchlogic test suite. I apologize for asking you to do this, but its a new policy I am implementing with my open source projects. I am just very busy and this would help me easily knock out the issue.
by Ben Johnson
binarylogic
Fri Aug 07 15:24:11 -0700 2009
| link
Sorting on a single table Help
Wilco, but I need time, since I am a newbie
by ramesh
binarylogic
Fri Aug 07 15:24:14 -0700 2009
| link
Sorting on a single table Help
Will this help ?
When I use :-
"@search = ArdData.new_search(params[:search]);" in the controller the sql generated on sorting any column is :- "Processing ArdDataController#index (for 127.0.0.1 at 2009-04-01 18:01:17) [GET] Parameters: {"search"=>{"order_as"=>"ASC", "order_by"=>"stock"}} ArdData Columns (2.1ms) SHOW FIELDS FROMard_datasArdData Load (0.2ms) SELECT * FROMard_datasORDER BYard_datas.stockASC LIMIT 25 SQL (0.1ms) SELECT count(ard_datas.part_number) AS count_part_number FROMard_datasRendering ard_data/index
Completed in 46ms (View: 23, DB: 3) | 200 OK [http://localhost/ard_data?search%5Border_as%5D=ASC&search%5Border_by%5D=stock]"Here I have tried sorting on the "Stock" column. This works for other columns also.
When I use:-
"@search = ArdData.new_search(:conditions => {:store_type => ’DOR’, :cpq_category => ’P’});" in the controller the sql generated on sorting any column is :-
"Processing ArdDataController#index (for 127.0.0.1 at 2009-04-01 19:24:09) [GET] Parameters: {"search"=>{"order_as"=>"ASC", "order_by"=>"stock"}} ArdData Columns (2.1ms) SHOW FIELDS FROM
ard_datasArdData Load (0.2ms) SELECT * FROMard_datasWHERE (ard_datas.cpq_category= ’P’ ANDard_datas.store_type= ’DOR’) LIMIT 25 SQL (0.6ms) SELECT count(ard_datas.part_number) AS count_part_number FROMard_datasWHERE (ard_datas.cpq_category= ’P’ ANDard_datas.store_type= ’DOR’) Rendering ard_data/index
Completed in 73ms (View: 28, DB: 3) | 200 OK [http://localhost/ard_data?search%5Border_as%5D=ASC&search%5Border_by%5D=stock]"The difference I see is in the "ArdData Load". The first query(no conditions) has a "ORDER BY" clause whereas the second(with conditions) doesn’t.
by ramesh
binarylogic
Fri Aug 07 15:24:17 -0700 2009
| link
Sorting on a single table Help
Sorry for the lack of activity, is this still an issue. It looks like this bug is for the older version of searchlogic.
by Ben Johnson
binarylogic
Tue Aug 18 22:32:17 -0700 2009
| link
Guess not, thanks!
-
2 comments Created 5 months ago by binarylogic@Ben Johnsonxfrom-lighthousexProblem with Multiple Conditions on STI Models|S| openxThe following is a result of the following example search condition
Ride.make_name_equals("Chevrolet").model_name_equals("Tahoe")
@@@ sql 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 MakeNow... I don’t think this is necessarily a "bug".... but how would one go about changing this
@@@ sql (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)
@@@ sql (make_models.name LIKE ’%Tahoe%’)) OR (make_models.name LIKE ’%Chevrolet%’) @@@
This ticket has 0 attachment(s).
Comments
binarylogic
Fri Aug 07 15:23:42 -0700 2009
| link
Problem with Multiple Conditions on STI Models
You could do:
Ride.make_name_equals_any("Chevrolet", "Tahoe")Does that fix the issue?
by Ben Johnson
binarylogic
Fri Aug 07 15:23:45 -0700 2009
| link
Problem with Multiple Conditions on STI Models
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..
@@@ ruby 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 end@@@
Where it would work like your order, just append search conditions based on links that a user can select from a list
by omarvelous
-
4 comments Created 5 months ago by binarylogic@Ben Johnsonxfrom-lighthousexnamed_scopexsearchxBug: Problems with multiple scopes beginning with the same name|S| openxPlease have a look at this small example:
@@@ ruby class Contact < ActiveRecord::Base
has_many :documents, :as => :object has_many :involvements has_many :documents_involved, :through => :involvements, :source => :object, :source_type => ’Document’ endContact.search(:documents_involved_title_like => ’foo’).all
=> NoMethodError: undefined method `involved_title_like’ for #<Class:...>
@@@
Using Rails 2.3.3 and searchlogic plugin 2.1.13 released 2009-07-29
This ticket has 0 attachment(s).
Comments
binarylogic
Fri Aug 07 15:23:13 -0700 2009
| link
Bug: Problems with multiple scopes beginning with the same name
I am trying to create a failing test for this in my test suite and can’t seem to do it. Do you mind giving that a shot. I am 90% certain this has to do with the order of keywords in the regular expression. Ex:
/(documents|documents_involved)/
When it should be
/(documents_involved|documents)/
This is a really easy fix, but I want to get a failing test in place first.
by Ben Johnson
binarylogic
Fri Aug 07 15:23:17 -0700 2009
| link
Bug: Problems with multiple scopes beginning with the same name
This has got a little bit more complicated because the bug has changed since searchlogic 2.3.3. Now it occurs not every time. I can’t reproduce it and can’t write a failing test, sorry. But it seems that it has something to do with other calls to Contact.search BEFORE - very strange.
But I have found a very similar issue, perhaps they are related. Here is a small patch for the specs (just add one line) and the specs will fail:
@@@ diff --git a/vendor/plugins/searchlogic/spec/spec_helper.rb b/vendor/plugins/searchlogic/spec/spec_helper.rb
index a2b98f0..c155938 100644
--- a/vendor/plugins/searchlogic/spec/spec_helper.rb +++ b/vendor/plugins/searchlogic/spec/spec_helper.rb @@ -71,6 +71,7 @@ Spec::Runner.configure do |config|class User < ActiveRecord::Base belongs_to :company, :counter_cache => true has_many :orders, :dependent => :destroyhas_many :orders_big, :class_name => ’Order’, :conditions => ’total > 100’ end
class Order < ActiveRecord::Base @@@
by Georg Ledermann
binarylogic
Sat Aug 08 10:13:28 -0700 2009
| link
Thanks
-
5 comments Created 5 months ago by binarylogic@Ben Johnsonxfrom-lighthousexorderxInconsistent result of "order" method|S| openxPlease have a look at this:
@@@ ruby
Document.search("descend_by_datetime" => true).order => nil
Document.search(:order => "descend_by_datetime").order => "descend_by_datetime"
Document.search(:order => "datetime DESC").order => "datetime DESC" @@@
IMHO the "order" method should always return the same result.
This ticket has 0 attachment(s).
Comments
binarylogic
Fri Aug 07 15:22:36 -0700 2009
| link
Inconsistent result of "order" method
You make a good point here, but for your examples to pass it would change the underlying logic behind the Search class. The point is just to store values that map to a named scope. So "decend_by_datetime" and :order => "descend_by_datetime" are 2 different named scoped. The order named scope is really just an alias that calls other named scopes. The whole point of it is to make searching with the search object easier. My suggestion is to use "order" when dealing with the search object, and if you aren’t you can do whatever you want.
What do you think?
by Ben Johnson
binarylogic
Fri Aug 07 15:22:41 -0700 2009
| link
Inconsistent result of "order" method
I’m trying to understand, hm, but there is still something confusing if I want to get the order of an existing search object. I find out that using proxy_options gives better result:
@@@ ruby
Document.search("descend_by_datetime" => true).proxy_options[:order] => "documents.datetime DESC" # OK!
Document.search("descend_by_datetime" => true).construct_finder_sql({}) => "SELECT * FROM
documentsORDER BY documents.datetime DESC" # OK! @@@@@@ ruby
Document.search(:order => "descend_by_datetime").proxy_options[:order] => "documents.datetime DESC" # OK!
Document.search(:order => "descend_by_datetime").construct_finder_sql({}) => "SELECT * FROM
documentsORDER BY documents.datetime DESC" # OK! @@@But what is this?
@@@ rubyDocument.search(:order => "datetime DESC").order => "datetime DESC" # OK!
Document.search(:order => "datetime DESC").proxy_options => {} # ???
Document.search(:order => "datetime DESC").construct_finder_sql({}) => "SELECT * FROM
documents" # ??? @@@You see that the proxy_options are blank and the last SQL statement has no ORDER BY.
Maybe I’m the only one who is confused, maybe it’s because I’m going on holiday tomorrow ;-)Best wishes,
Georgby Georg Ledermann
binarylogic
Fri Aug 07 15:22:43 -0700 2009
| link
Inconsistent result of "order" method
That’s because the :order condition MUST be a named scope, you can’t pass raw SQL.
by Ben Johnson
binarylogic
Fri Aug 07 15:22:45 -0700 2009
| link
Inconsistent result of "order" method
Also, this is to protect from SQL injections
by Ben Johnson
binarylogic
Fri Aug 07 15:22:48 -0700 2009
| link
Inconsistent result of "order" method
Ok, totally agree. To avoid irritations, what about raising an exception (like "UnknownOrderError") if someone gives raw SQL (or unknown order scope) to the :order key? I think this would be a good thing, because there is already a Searchlogic::Search::UnknownConditionError raised for wrong :condition value.
by Georg Ledermann
-
Comments
kalasjocke
Sun Aug 09 15:21:45 -0700 2009
| link
Just add the table name to the default scopes order, its kind of uggly but it works.
default_scope :order => "companies.name"
for example.
binarylogic
Wed Aug 19 14:38:58 -0700 2009
| link
Yeah, kalas has it right. Thanks.
-
order Helper doesn't honor existing search params
2 comments Created 6 months ago by willcodeforfooChanging the end of order() to something like the following will work:
new_params = params[options[:params_scope]].merge({:order => new_scope}) link_to options[:as], url_for(options[:params_scope] => new_params), html_optionsIt worked for my simple app, not sure if its a comprehensive solution, though.
Comments
it's already fixed for 2.0.2. See http://github.com/binarylogic/searchlogic/commit/c2c7c9374deeb9cb6ccaa072d795a4f1985df00d
binarylogic
Sun Aug 09 01:50:09 -0700 2009
| link
Thanks.
-
s = Person.search s.ascend_by_name true s.all | Andy | Bob | Charlie . . . . . s.descend_by_name true s.all | Andy | Bob | Charlie . . . . . s.ascend_by_name false s.all | Zita | Yvonne | Xaviere . . . . .
Comments
why you need to use true or false? Just use ascend_by_name or descend_by_name and you will be fine.
binarylogic
Sun Aug 09 01:49:50 -0700 2009
| link
Im not sure what the question is.
-
I love searchlogic, but it leaves out conditions with Array or Range values. For instance, a named scope may look like:
class State < ActiveRecord::Base named_scope :by_abbreviation, lambda{|a| {:conditions => {:abbreviation => a}} } endOne could pass a number of different types of arguments to such a scope:
State.by_abbreviation('MI') State.by_abbreviation(['MI', 'WA']) State.by_abbreviation('MA'..'MZ')These produce the following queries, respectively:
SELECT * FROM `states` WHERE (`states`.`abbreviation` = 'MI') SELECT * FROM `states` WHERE (`states`.`abbreviation` IN ('MI','WA')) SELECT * FROM `states` WHERE (`states`.`abbreviation` BETWEEN 'MA' AND 'MZ')Could these sorts of scopes be built into searchlogic, using "in" and "between" shorthand in the scope names?
Comments
binarylogic
Sun Aug 09 01:49:02 -0700 2009
| link
Thats a good idea, I will see what I can do to get this in there.
+1 -- I've been scratching my head on this one for a while. The scope works in my console when I run it directly, but in the app - which involves searchlogic - the scope doesn't work.
named_scope :occurred_at_between, lambda {|*args| {:conditions => ["alerts.occurred_at >= ? AND alerts.occurred_at <= ?", (args.first), (args.last)]}}This works in the console, but when added to a search object, it only gets one parameter.
binarylogic
Tue Aug 18 22:36:18 -0700 2009
| link
The problem is that the values sent from the form can only be strings, arrays, or hashes. You can't send ranges, etc. In the console this is dioble, but to have range support directly from the params hash is not possible. The only solution is to send an array to the scope and use the first and last values. You could also create an alias scope that handled a single value and translated to whatever scope you wanted to use.
laserlemon
Wed Aug 19 05:36:25 -0700 2009
| link
That's generally true, but it is entirely possible that a custom param parser is being used.
For instance, I use http://github.com/laserlemon/search_party which gives me search_params containing arrays, ranges, money objects, floats, etc.
Also, searchlogic is obviously much more useful than just passing params to named_scopes. The collection of scopes it adds to the picture are half the fun. In my case, I needed to pass a possible range value to a scope that queried a model's association. I really would rather not reinvent the wheel as far as the association support that's already been built in.
laserlemon
Wed Aug 19 08:33:59 -0700 2009
| link
I forked and addressed the issue using AR's existing attribute_condition and expand_range_bind_variables methods, so the behavior is consistent.
binarylogic
Wed Aug 19 13:52:54 -0700 2009
| link
I agree, thanks a lot for your commits I applied them.
-
I've put some of SL initialization values into rails initializers via Searchlogic::Config. Now in 2.0 it's missing.
Comments
binarylogic
Sun Aug 09 01:48:09 -0700 2009
| link
SL 2.0 trimmed a lot of fat, because of this no configuration is needed as of yet. Any helper configuration is passed to the helper directly, and the only helper present is order.
-
scope on a column beginning with the name of an association
4 comments Created 6 months ago by etaqueGiving this models :
User has_many :classifieds
ClassifiedUser model has the column "classifieds_count".
This search throws an error, because it seems to ignore User.classifieds_count column and to search on the associated Classified model :
User.classifieds_count_greater_than(5)Emilien
Comments
Has anyone got a quick fix for this? I would love to have it!!
binarylogic
Sun Aug 09 01:47:09 -0700 2009
| link
This has been fixed, let me know if you have any other issues.
-
Reverse named_scopes not only for just equal
2 comments Created 6 months ago by slainer68It would be very cool to also have "reverse" named_scope for all conditions, not only equal.
For example :
User.username_not_blank User.username_not_empty User.username_does_not_end_with("bjohnson") User.username_does_not_begin_with("bjohnson")and so on...
I may try to help for this :).
Nicolas
Comments
Bigtime. I've run into this with LIKE, and got suspicious results (duplicated rows) when I tried it myself across a join.
binarylogic
Sun Aug 09 01:46:42 -0700 2009
| link
This has been in searchlogic for a couple of weeks now, sorry for not noticing this. But you should be good to go.
- @Ben Johnson▾
- associations▾
- conditions▾
- from-lighthouse▾
- group▾
- grouping▾
- named_scope▾
- order▾
- order_by▾
- search▾
- |S| new▾
- |S| open▾
- Apply to Selection
-
Change Color…
Preview:preview
- Rename…
- Delete






This is passing for me:
it "should fix bug for issue 26" do count1 = User.id_ne(10).username_not_like("root").count count2 = User.id_ne(10).username_not_like("root").count count1.should == count2 endCan you write a failing test?