This is another way of querying in Ruby.
Add this line to your application's Gemfile:
gem 'the_where'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install the_where
This Library set default params process for where query in ActiveRecord
- Params:
# rails 4 and later, the_where does nothing
params = { role_id: 1, age: 20 }
User.the_where(params)
- params
User.belongs_to :role
params = { name: 'dhh', 'role.id': 2 }
# you can use any table name or reference name
params = { name: 'dhh', 'roles.id': 2 }
- Before use
the_where
User.includes(:student).where(name: params[:name], role: {id: params[:'role.id']})
- After Use
the_where
User.the_where(params)
- params
params = { 'role_id-lte': 2 }
- Before use
the_where
User.where('role_id >= ?', params[:'role_id-lte'])
- After use
the_where
User.the_where(params)
- params
params = { age: '', role_id: 1 }
- Before use
the_where
users = User.where(role_id: params[:role_id])
users = users.where(age: params[:age]) if params[:age]
- After use
the_where
User.the_where(params)
# also can control which blank value can use
User.the_where(params, { allow: [nil] })
- Params
params = { 'age-asc': '1', 'last_login_at-asc': '2' }
- Before use
the_where
User.order(age: :asc, last_login_at: :asc)
- After use
the_where
User.the_where(params)
- Params
{ name: 'dhh', 'role.id': 2, 'age-lte': 2, 'age-asc': '1', 'last_login_at-asc': '2' }
- Before use
the_where
User.includes(:role).where(name: params[:name], 'roles.id': params[:'role.id']).order(age: :asc, last_login_at: :asc)
- After use
the_where
User.the_where(params)
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/gith-u-b/the_where. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the TheWhere project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.