开发者

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 c开发者_如何转开发heat sheet online somewhere?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜