Skip to content

Instantly share code, notes, and snippets.

@CodePint
Created June 11, 2018 19:52
Show Gist options
  • Save CodePint/dd39072d9859a26d18f18e7dbb7526f3 to your computer and use it in GitHub Desktop.
Save CodePint/dd39072d9859a26d18f18e7dbb7526f3 to your computer and use it in GitHub Desktop.
search climbs by gym
class Climb < ApplicationRecord
include PgSearch
belongs_to :user
belongs_to :route
has_many :comments
end
class Gym < ApplicationRecord
include PgSearch
has_many :routes
has_many :memberships
has_many :users, :through => :memberships
end
class Route < ApplicationRecord
include PgSearch
belongs_to :gym
belongs_to :user
has_many :climbs
has_many :comments
end
Task: find which climbs are set at a particular gym
example:
@gym = gym.find(name: "stronghold")
@routes = Climb.where(gym_id: @gym.id)
@CodePint
Copy link
Author

has_many :climbs, :through => :routes
has_one :gym, :through => :routes

@CodePint
Copy link
Author

^ STILL doesnt do quite what i want. allows me to do @gym.climbs but not Climb.where(gym_id: @gym.id)

@climbs.joins(:route).where(gym_id: @gym.id)
I know something like this ^ should maybe work as well.

@bjpirt
Copy link

bjpirt commented Jun 12, 2018

What's wrong with @gym.climbs ?

@CodePint
Copy link
Author

absolutely nothing....ive got to stop doing 10 hours in front of a screen. (just picked this back up after yesterday)
I think it felt alien to be doing the lookup on gyms instead of climbs, this was part of a search function im finishing.

sorry for wasting your time.

side note, im really enjoying postgres. got this up n running:

#pg search scope for route identifier
pg_search_scope(
:search_identifier,
:against => %i(identifier),
:using => {
:tsearch => {:prefix => true},
:trigram => {:threshold => 0.1}
}
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment