Last active
August 29, 2015 14:10
-
-
Save 8vius/568072b6328d03e2fb98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Venue < ActiveRecord::Base | |
| belongs_to :location, dependent: :destroy | |
| has_and_belongs_to_many :categories | |
| has_many :yums | |
| has_one :contact, inverse_of: :venue, dependent: :destroy | |
| belongs_to :owner, foreign_key: :owner_id, class_name: "User" | |
| belongs_to :creator, foreign_key: :creator_id, class_name: "User" | |
| validates :name, presence: true | |
| validates :creator_id, presence: true | |
| validates :foursquare_id, uniqueness: { allow_blank: true } | |
| accepts_nested_attributes_for :categories | |
| accepts_nested_attributes_for :contact | |
| include PgSearch | |
| pg_search_scope :search, against: [:name], | |
| using: { | |
| tsearch: { | |
| dictionary: "english", | |
| prefix: true | |
| } | |
| }, | |
| associated_against: { | |
| location: [:street, :city, :postal_code, :state, :country], | |
| categories: [:name] | |
| } | |
| pg_search_scope :search_by_categories, | |
| using: { | |
| tsearch: { | |
| dictionary: "english" | |
| } | |
| }, | |
| associated_against: { | |
| categories: [:name] | |
| } | |
| def self.near(lat, lng) | |
| location_ids = Location.near([lat, lng], 1, units: :km).order("distance").map(&:id) | |
| if !location_ids.empty? | |
| values = [] | |
| location_ids.each_with_index do |id, index| | |
| values << "(#{id}, #{index + 1})" | |
| end | |
| joins("JOIN (VALUES #{values.join(",")}) as x (id, ordering) ON venues.location_id = x.id").order('x.ordering') | |
| else | |
| none | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment