Jhorra
Jhorra

Reputation: 6321

Question about queries with multiple where clauses, Rails 3

I have a class called Gym

class Gym < ActiveRecord::Base
  has_many :gym_users
  has_many :users, :through => :gym_users

  validates :name, :address1, :city, :state, :zip, :phone, :website, :presence => true

  def has_admin_access?(user)
    user.present? && gym_users.where(:user_id => user.id, :gym_role_id => 3)
  end
end

I want to modify the has_admin_access method to look for a gym_role_id of 3 or 4. The other question I have is how do I do a less than or greater than?

Is there a cheat sheet online somewhere?

Upvotes: 0

Views: 4265

Answers (1)

Aj Gu
Aj Gu

Reputation: 1439

Try this

user.present? && gym_users.where(:user_id => user.id, :gym_role_id => [3, 4])

Try MetaWhere gem for writing less than and great than queries along with symbols. Tutorial available at http://railscasts.com/episodes/251-metawhere-metasearch

MetaWhere isn't supported as of Rails 3.1. A more up to date alternative is Squeel.

Upvotes: 5

Related Questions