私の Rails アプリには、1 対多の関係を形成する 2 つのモデルがあります。最初のモデルである store は実店舗を表し、'geocoder' gem と住所列を使用して正しくジオコーディングされた緯度と経度の列があります。ストアには_多くの商品があります。この商品には has_one ストアがあります。ユーザーが検索フォームに入力した住所への近さに基づいて、製品のインデックスを返したいと思います。私のコードの関連部分と、検索を実装しようとする試みは次のとおりです。
schema.rb:
create_table "products", force: true do |t|
t.string "title"
...
t.integer "store_id"
end
create_table "stores", force: true do |t|
...
t.string "business_address"
t.float "latitude"
t.float "longitude"
end
store.rbで
class Store < ActiveRecord::Base
has_many :products
geocoded_by :business_address
after_validation :geocode, :if => :business_address_changed?
end
product.rbで
class Offer < ActiveRecord::Base
belongs_to :store
end
ビュー/products/search.html.erbで
...
<%= form_tag products_path, :method => 'get' do %>
<p>
Find products near<br />
<%= text_field_tag :custom_address, params[:custom_address] %><br />
</p>
<p>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
products_controller.rb で
def index
@products = Store.near(params[:custom_address], 100, order: :distance, :select => "products.*")
end
上記の index メソッドは、
ActiveRecord::StatementInvalid in Products#index
エラー
続行する方法がわかりません。明らかに、私が Near メソッドと :select を使用している方法に問題がありますが、頭を包むことはできません。距離別に商品を返品するにはどうすればよいですか?
データベース アダプタとして MySQL を使用しています。SQLite での三角関数の欠如による問題について聞いたことがあります。