错误的数据被插入到我的搜索模型sqlite数据库中。对于一个特定列(:city),数据输入为“--- \ n-''\ n- string \ n”,而不是“字符串”。此外,正在为neighborhood_id自动插入数字“1”。更新:我通过删除city列并添加city_id和BTHM关系解决了字符串问题。现在为neighborhood_id和city_id插入了数字“1”。以下是视图中的集合选择代码:
<%= 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 %>
选择转到存储所有搜索参数的搜索模型。以下是City模型的架构:
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
这是搜索控制器(感谢Yule的建议):
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
在我的开发环境中,当我选择'Algonquin'时,它会像这样插入数据库:
=> #<Search id: 322, city: "---\n- ''\n- Algonquin\n",
此外,即使没有选择邻域,neighborhood_id也始终设置为“1”!
这是控制台输出,显示输入参数以及如何为neighborhood_id和city_id保存“1”。在这种情况下,我选择了3间卧室和城市'Algonquin',现在有city_id =&gt; 1148,我没有选择任何社区。 p>
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]]
City表很干净。
有没有人对于为什么会发生这种情况有任何想法,或者某些策略来诊断或解决问题?任何帮助将不胜感激!
答案 0 :(得分:0)
<li>Select City <%= f.select(:city, City.order(:name).collect(&:name), {:prompt => "Enter Cities"}, {:multiple => true})%></li>