Rails活动记录没有返回我期望的内容

时间:2013-03-13 05:30:06

标签: ruby-on-rails ruby ruby-on-rails-3

我有一个包含4个位置的Locations表。架构如下:

create_table "locations", :force => true do |t|
    t.string   "name"
    t.string   "address"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

我在ajax调用中运行它并且它在我的下面的put行中继续返回Location

@current_location = Location.where('lower(name) = ?', params[:id][1..-1].humanize.downcase)     
p "@current_location = #{@current_location}"

params[:id][1..-1].humanize.downcase解释为'brooklyn park'

我实际上并没有在日志文件中找到此查询,当我将活动记录复制并粘贴到rails控制台时,它似乎工作正常。

为什么会一直返回'Location'

1 个答案:

答案 0 :(得分:1)

替换

@current_location = Location.where('lower(name) = ?', params[:id][1..-1].humanize.downcase)

使用

@current_location = Location.where('lower(name) = ?', params[:id][1..-1].humanize.downcase).first