0

错误数据被插入到我的搜索模型 sqlite 数据库中。对于一个特定的列 (:city),数据输入为“---\n- ''\n- string\n”,而不是“字符串”。此外,将自动为neighbor_id 插入数字“1”。更新:我通过删除 city 列并添加 city_id 和 BTHM 关系解决了字符串问题。现在为neighbor_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",

此外,即使没有选择邻居,neighbor_id 也始终设置为“1”!

这是控制台输出,显示了输入参数以及如何为neighbor_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]]

City 餐桌很干净。

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

4

1 回答 1

0
<li>Select City <%= f.select(:city, City.order(:name).collect(&:name), {:prompt => "Enter Cities"}, {:multiple => true})%></li>
于 2012-02-24T15:28:50.157 回答