Implement state search with new model
This commit is contained in:
@@ -5,7 +5,7 @@ module Api::V1
|
||||
def index
|
||||
parks = Park.all
|
||||
if params[:state].present?
|
||||
parks = parks.where("states like '%' || ? || '%'", params[:state])
|
||||
parks = parks.joins(:states).where(parks_by_states: { state: params[:state] })
|
||||
end
|
||||
render json: {
|
||||
total: parks.count,
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
class Park < ApplicationRecord
|
||||
has_many :alerts, foreign_key: :park_code, primary_key: :code,
|
||||
inverse_of: :park, dependent: :destroy
|
||||
has_many :states, class_name: "ParksByState",
|
||||
foreign_key: :park_code, primary_key: :code,
|
||||
inverse_of: :parks, dependent: :destroy
|
||||
end
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
class ParksByState < ApplicationRecord
|
||||
has_many :parks, foreign_key: :park_code, primary_key: :code,
|
||||
inverse_of: :states
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user