잘못된 검색 모델 sqlite 데이터베이스에 데이터가 삽입되고 있습니다. "문자열"대신 데이터가 특정 열 (: 도시)에 대해 "--- \ n-" '\ n- 문자열 \ n "으로 입력됩니다. 또한 neighborhood_id에 '1'숫자가 자동으로 삽입됩니다. 업데이트 : 도시 열을 삭제하고 city_id 및 BTHM 관계를 추가하여 문자열 문제를 해결했습니다. 이제 'neighborhood_id'와 'city_id'둘 다에 '1'숫자가 삽입됩니다. 보기의 콜렉션 선택 코드는 다음과 같습니다.sqlite 데이터베이스에 추가 데이터가 삽입되고 있습니다
<%= form_for @search do |f| %>
<li>Select City <%= f.collection_select(:city, City.order(:name), :name, :name, {:prompt => "Enter Cities"}, {:multiple => true})%></li>
<li>Select Neighborhood(s)<%= f.collection_select(:neighborhood_id, Neighborhood.order(:name), :id, :name, {:prompt => "Enter Chicago Neighborhood(s)"}, {:multiple => true}) %></li>
<li><%= f.submit 'Search' %> </br><%= link_to "Reset Search", root_path %></li>
<% end %>
선택 사항은 모든 검색 매개 변수를 저장하는 검색 모델로 이동합니다.
create_table "cities", :force => true do |t|
t.string "name"
t.timestamp "created_at"
t.timestamp "updated_at"
end
그리고 검색 모델에 대한 스키마 : 여기
create_table "searches", :force => true do |t|
t.string "city"
t.integer "beds"
t.decimal "baths"
t.integer "price"
t.string "name"
t.timestamp "created_at"
t.timestamp "updated_at"
t.integer "neighborhood_id"
end
(제안에 대한 감사 성탄절) 검색 컨트롤러 :
class SearchesController < ApplicationController
def index
@searches = Search.all
end
def show
@search = Search.find(params[:id])
@user = current_user unless current_user.nil?
@search.save
@listings = @search.listings
if @listings.blank?
else
@listings = @listings.flatten
@no_search_results = @listings.count
@json = @listings[0,250].to_gmaps4rails do |listing, marker|
marker.json "\"id\": #{listing.id}"
end
end
end
def new
@search = Search.new
@search.save
redirect_to @search
end
def create
@search = Search.new(params[:search])
@search.user_id = session[:user_id] unless session[:user_id].nil?
@search.save
redirect_to @search
end
def destroy
@search = Search.find(params[:id])
@search.destroy
end
def edit
end
def update
@search = Search.find(params[:id])
@search.update_attributes(params[:search])
redirect_to @search
end
end
여기에시 모델에 대한 스키마입니다
다음은 검색 모델입니다.
class Search < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :neighborhoods
has_and_belongs_to_many :cities
scope :named, where("name IS NOT NULL") # for view: only show searches for user where user has saved a name.
def listings
@listings ||= find_listings
end
private
def find_listings
i = 0
batch_size = 4000
max_return = 150
total_listings = 0
db_size = Listing.count
search_results =[]
while total_listings < max_return && batch_size*i <= db_size do
listings = Listing.order(:price).limit(batch_size).offset(i*batch_size).scoped
if neighborhood_id.present?
listings = listings.where(:neighborhood_id => neighborhood_id)
end
if city.present?
listings = listings.where("city LIKE ?", city)
end
i = i + 1
search_results << listings
if search_results.present?
total_listings = total_listings + search_results.flatten.count
else
end
end
if search_results.present?
listings = search_results.flatten[0..149]
else
return nil
end
end
end
내 개발 환경에서 363,210
, 나는 '알곤 퀸'를 선택하는 경우는 다음과 같이 데이터베이스에 삽입됩니다 :
=> #<Search id: 322, city: "---\n- ''\n- Algonquin\n",
또한, neighborhood_id 항상 '1'로 설정되어 이웃이 전혀 선택되지 않은 경우에도!
다음은 입력 매개 변수와 neighborhood_id 및 city_id에 '1'이 저장되는 방법을 보여주는 콘솔 출력입니다. 이 시나리오에서 저는 3 개의 침실과 도시 'Algonquin'을 선택했습니다. 현재 city_id => 1148이고 어떤 이웃도 선택하지 않았습니다.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cS4xGLsBZJASlEmexmR2tUSMV+doBX30C14jHFRDqTA=", "search"=>{"city_id"=>["", "1148"], "neighborhood_id"=>[""], "max_distance"=>"", "m_max_distance"=>"", "beds"=>"3", "baths"=>"", "min_price"=>"", "price"=>"", "user_id"=>""}, "commit"=>"Search"}
(0.1ms) begin transaction
SQL (1.1ms) INSERT INTO "searches" ("baths", "beds", "city_id", "created_at", "min_price", "neighborhood_id", "price", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["baths", nil], ["beds", 3], ["city_id", 1], ["created_at", Sat, 25 Feb 2012 08:15:04 CST -06:00], ["min_price", nil], ["neighborhood_id", 1], ["price", nil], ["updated_at", Sat, 25 Feb 2012 08:15:04 CST -06:00], ["user_id", nil]]
도시 테이블은 깨끗합니다.
왜 이런 일이 벌어지고 있는지 또는 문제를 진단하거나 해결할 수있는 방법에 대한 아이디어가 있습니까? 어떤 도움을 많이 주시면 감사하겠습니다!
컨트롤러 코드가 유용 할 수 있습니다. – Yule
'검색'모델의 코드를 게시 할 수 있습니까? –