Skip to content
This repository

DISCONTINUED - An ActiveRecord plugin for dealing with complex search :conditions.

tree: 108ae8f01c

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 CHANGELOG
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 TODO
Octocat-spinner-32 setup.rb


MultiConditions is a simple ActiveRecord plugin for storing ActiveRecord query and make complex queries painless.


This plugin doesn't replace ActiveRecord#with_scope method, nor the basic :condition usage but extends it with the ability of storing illimitate conditions in multiple step.

conditions =
# ... do some elaboration
conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo']
# ... other elaboration
conditions.append_condition(['name = ?', 'aname']

# => "active = true AND query LIKE '%foo' AND name = 'aname'"


  • Ruby 1.8.6

  • ActiveRecord 2.0 (or greater)

If you want to run the test suite:

  • sqlite3-ruby


MultiConditions source code is managed via GIT and hosted at GitHub:

Download and Installation

MultiConditions is at the beginning of development stage. It's stable enough to be used, but at the moment the only way to install it is to grab the source code and install it manually.

In a few days it will be packaged as a GEM. Stay tuned!


Create a new instance

As stated by the Important section above, you first need a valid ActiveRecord model to create a MultiConditions instance. Because MultiConditions helps you to deal with ActiveRecord queries, it's easy to understand why you MUST establish a valid database connection and have at least one table (mapped with a Model) to query.

If you use ActiveRecord from Rails, this is just a matter of creating a new Model.

# create the Task model
class Task < ActiveRecord::Base

Now MultiConditions is automatically available within your Task namespace. You can use it in whatever class method, for example:

class Task < ActiveRecord::Base

  def complex_search()
    c = => 'bar')
    Task.find(:all, c.to_conditions)


But you can also create a new instance from an other library, class or model. Just remember to initialize MultiConditions from its own namespace.

class Foo
  class << self
    def my_cool_conditions => 1).to_conditions

# => 'foo = 1'

Appending conditions

You can append new conditions calling

  • #append_condition

  • #prepend_condition

and passing the conditions you want to append or prepend. See Condition Types section to lean more about supported objects.

conditions =
conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo']
conditions.prepend_condition(['name = ?', 'aname']

# => "name = 'aname' AND active = true AND query LIKE '%foo'"

Condition types

The MultiConditions object accepts any type of conditions supported by ActiveRecord, including Strings, Arrays and Hashes, and merges them alltogether just before sending the final :condition value to ActiveRecord search method.

conditions =
conditions.append_conditions(:foo => 1, :bar => 2)
conditions.append_conditions('active = 1')
conditions.append_conditions(['name LIKE ?', '%foo'])

# => 'foo = 1 AND :bar = 2 AND active = 1 AND name LIKE '%foo'

See ActiveRecord::Base#find documentation for more conditions examples.


Once loaded, this library become part of ActiveRecord package and creates its own namespace at ActiveRecord::Base::MultiConditions.

For various reason, you cannot initialize a new ActiveRecord::Base::MultiConditions but you MUST initialize a MultiConditions instance from a Model.

# The wrong way
# raises Message: <"undefined method `abstract_class?' for Object:Class">

# The right way
class Model < ActiveRecord::Base

  def a_method()
    c =
    find(:all, :conditions => c.to_conditions)



Website and Project Home

FeedBack and Bug reports

Feel free to email Simone Carletti with any questions or feedback.

Please submit your bug reports to the Redmine installation for MultiConditions available at



Something went wrong with that request. Please try again.