小编Luk*_*ann的帖子

错误:运算符不存在:字符变化=整数

我面临一个共同的问题.我的Rails应用程序在我的本地计算机上运行,​​但在部署到heroku后它崩溃了:

<% unless @user.hotels.empty? %>
  <% @user.hotels.each do |hotel| %>
    <%= "#{hotel.description} #{hotel.name} in #{hotel.city}, #{hotel.country}" %><br />
  <% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

这是来自heroku日志:

ActionView::Template::Error (PGError: ERROR:  operator does not exist: character varying = integer
LINE 1: SELECT "hotels".* FROM "hotels" WHERE ("hotels".user_id = 1)
                                                                ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "hotels".* FROM "hotels" WHERE ("hotels".user_id = 1)):
Run Code Online (Sandbox Code Playgroud)

@user.hotels.empty?创建错误.我知道,sqlite非常宽容,但PostgreSQL不是.这是酒店模型中的外键:user_id :integer

Heroku说:

Make …
Run Code Online (Sandbox Code Playgroud)

sqlite postgresql heroku ruby-on-rails-3

13
推荐指数
1
解决办法
2万
查看次数

排序包含ruby/rails中的数字的字符串

我想根据他们有多少facebook喜欢对我的所有位置进行排序.但facebook_likes不是integer,它是一个string.

这是我使用的rails代码: @locations = Location.order("facebook_likes ASC").all

现在我得到这样的东西:

  • 10
  • 100
  • 201
  • 2
  • 304
  • 400000
  • 50
  • ...

如何根据值进行排序,以便签到次数最多的位置位于顶部.从一开始就使用字符串而不是整数来解决这个问题吗?

谢谢你的帮助!

ruby string integer ruby-on-rails-3

8
推荐指数
1
解决办法
6259
查看次数

Rails 3使用NOT NULL在.where条件之后排序

我有一个显示最快用户的排名:

@users = User.find.sort { |a, b| b.finished_at <=> a.created_at }
Run Code Online (Sandbox Code Playgroud)

现在我要添加一些代码以防止因为finished_at beeing而导致错误,nil直到用户完成.

@users = User.find(:all, :conditions => "finished_at IS NOT NULL").sort { |a, b| b.finished_at <=> a.created_at }
Run Code Online (Sandbox Code Playgroud)

但是sort不起作用.这种链接方法有缺陷吗?而且是find(:all, :conditions => "finished_at IS NOT NULL")轨道3路或过时?

提前致谢

ruby sorting where conditional-statements ruby-on-rails-3

3
推荐指数
1
解决办法
9119
查看次数

Heroku Ruby NoMethodError string.capitalize

我使用以下代码根据第一个字母对位置进行分组.

mobile_controller:

def index
  @locations = Location.all.group_by{|l| l.name[0].capitalize.match(/[A-Z]/) ? l.name[0].capitalize : "#"}
end
Run Code Online (Sandbox Code Playgroud)

视图:

<% @locations.keys.sort.each do |starting_letter| %>
  <%= starting_letter %>
  <% @locations[starting_letter].each do |location| %>
    <%= location.name %>        
  <% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

一切都在我的本地机器上工作正常,但heroku不喜欢它并不断向我显示这个错误:

NoMethodError (undefined method `capitalize' for 66:Fixnum):
app/controllers/mobile_controller.rb:13:in `search'
app/controllers/mobile_controller.rb:13:in `search'
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

提前致谢

解决方案:将 我的Heroku Stack更新为Ruby 1.9.

ruby heroku nomethoderror ruby-on-rails-3

3
推荐指数
1
解决办法
625
查看次数