ruby - Trying to solve for a '.includes' issue in rails controller active record -
i pretty noob ror coder, trying figure out problem no luck. have application accessing 2 models:
-appointments (belongs location) -locations (has many appointments)
on appointment index page displaying map of locatations pins using gmap4rails gem. working fine, cannot figure out how limit @appointments locations nearby...
def index @appointments = appointment.where(active: "true").order("created_at desc") @hash = gmaps4rails.build_markers(@appointments) |appointment, marker| marker.lat appointment.location.latitude marker.lng appointment.location.longitude end end
this gets me of appointments active. working on locations index:
def index if params[:search].present? @locations = location.near(params[:search], 50) else @locations = location.near([session[:latitude], session[:longitude]], 50) end @hash = gmaps4rails.build_markers(@locations) |location, marker| marker.lat location.latitude marker.lng location.longitude end end
i have tried every variation of query include appointment.location.near...
what missing here???
here models:
class appointment < activerecord::base belongs_to :user belongs_to :profile belongs_to :length belongs_to :location has_many :confirmations validates_presence_of :title, :comments end
locations...
class location < activerecord::base geocoded_by :address def address [address_1, city, state, zip].compact.join(', ') end after_validation :geocode, :if => :address_1_changed? has_many :profile_locations has_many :profiles, through: :profile_locations has_many :appointments end
try this:
# appointments controller def index nearby_locations = location.near([session[:latitude], session[:longitude]], 50) @appointments = appointment.where(location: nearby_locations).where(active: true).order(created_at: :desc) ... end
note can pass array of objects/ids (like nearby_locations above) collection of objects (@appointments above). can chain queries multiple wheres.
Comments
Post a Comment