2012-02-24 179 views
0

坏数据被插入到我的搜索模型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]] 

城市的桌子很干净。

有没有人有任何想法,为什么发生这种情况,或一些策略来诊断或解决问题?任何帮助将非常感激!

+0

您的控制器代码在这里可能有用 – Yule 2012-02-24 15:06:56

+0

您可以发布您的“搜索”模型的代码吗? – 2012-02-24 15:16:04

回答

0
<li>Select City <%= f.select(:city, City.order(:name).collect(&:name), {:prompt => "Enter Cities"}, {:multiple => true})%></li> 
+0

这产生完全相同的结果。而不是“阿尔冈金”作为:城市,模型显示“--- \ n-''\ n-''\ n- Algonquin \ n” – tbone 2012-02-24 22:14:08

+0

有可能是城市记录本身包含像“ - \ n-''\ n-''\ n- Algonquin \ n“'。你可以检查城市里面的数据表 – 2012-02-25 12:36:54

+1

是的,我做了,城市表是干净的。这个语法“---/n-”....看起来像序列化语法,我删除了:city列,添加了一个:city_id列,并创建了一个属于has_many的关系,问题解决了....尽管我不'我不知道问题是如何开始的!我仍然有一个问题,即在没有选择任何内容的情况下,数字'1'被插入到neighborhood_id字段和现在的city_id字段中!另外,如果我选择其他城市或邻居,结果仍然是city_id => 1和neighborhood_id => 1.非常奇怪。 – tbone 2012-02-25 13:57:49

相关问题