壞數據被插入到我的搜索模型sqlite數據庫。對於一個特定的列(:城市),數據被輸入爲「--- \ n-''\ n-字符串\ n」而不是「字符串」。此外,爲鄰域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
在我的開發環境中,當我選擇「阿岡昆」它被插入到數據庫中是這樣的:
=> #<Search id: 322, city: "---\n- ''\n- Algonquin\n",
此外,neighborhood_id始終設置爲「1」,即使附近沒有在所有選擇!
這是控制檯輸出,顯示輸入參數以及如何爲neighborhood_id和city_id保存'1'。在這種情況下,我選擇了3間臥室和城市'阿爾岡昆',現在有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 2012-02-24 15:06:56
您可以發佈您的「搜索」模型的代碼嗎? – 2012-02-24 15:16:04