0
私は現在、この検索を使って検索していますtutorial。すべてがうまくいき、2つの異なるテーブル(School &地区)からさまざまなリソースを検索できます。検索モデルでコードを組み合わせるにはどうすればよいですか?
しかし、私は現在、私のコントローラから2つの別個の検索メソッドを呼び出し、検索結果を自分のコントローラの1つの配列に結合しています。
私のモデルにコードを組み合わせるのにとにかくありますか?
検索コントローラー
def show
@search = Search.find(params[:id])
@searches = []
///Calls two separate search methods from my model and combines the results.
(@searches << @search.district_resources).flatten!
(@searches << @search.school_resources).flatten!
@searches = @searches.uniq
respond_to do |format|
format.json { render :json => @searches.to_json }
end
end
方法
//How can I combine this code to search for values in both tables?
def district_resources
@district_resources ||= find_district_resources
end
def school_resources
@school_resources ||= find_school_resources
end
def find_school_resources
school_resources = SchoolResource.order(:name)
school_resources = school_resources.where(state_id: state_id) if state_id.present?
end
def find_district_resources
district_resources = DistrictResource.order(:name)
district_resources = district_resources.where(state_id: state_id) if state_id.present?
end
2つのモデルはどのように関連していますか? STIを使用していますか? – oreoluwa