2012-02-24 210 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

相關問題