Rails Rails 升级遇到的问题。从 3.2.2 到 4.2.5

xiaoping_rubyist · 2016年03月14日 · 最后由 msg7086 回复于 2016年03月15日 · 2271 次阅读

同样的代码在不同的版本里被解析出来的结果为啥不一致呢,同一个数据库,代码都一样,rails 行为不一样。 controller

def index 
    @user = get_user
    @role = get_user_role
    @locations = Location.all.map { |l| l[:locationid] }
    params[:location] = session[:location] if !session[:location].nil?
    unless params[:location].nil? 
      @locations = [Location.where({:name => OSUtils.get_location_name(params[:location])}).first.locationid]
    end
   @topologies = Topology.find(:all, 
         :conditions => ["status != ? AND testbed_id IN (?) AND location_id IN (?)", 
         'TERMINATED', Testbed.where(:tb_type => "OSEXEC").all.map {|t| t.tbid}, @locations ])
end

log in rails 4.2.5

Started GET "/topologies" for 10.103.201.205 at 2016-03-14 15:09:30 +0800
Processing by TopologiesController#index as HTML
  Location Load (333.7ms)  SELECT "location".* FROM "location"
  Location Load (332.6ms)  SELECT  "location".* FROM "location" WHERE "location"."name" = $1  ORDER BY "location"."locationid" ASC LIMIT 1  [["name", "Shanghai"]]
  Testbed Load (672.7ms)  SELECT "testbed".* FROM "testbed" WHERE "testbed"."tb_type" = $1  [["tb_type", "OSEXEC"]]
Completed 404 Not Found in 1682ms (ActiveRecord: 1672.6ms)

ActiveRecord::RecordNotFound (Couldn't find all Topologies with 'topology_id': (all, {:conditions=>["status != ? AND testbed_id IN (?) AND location_id IN (?)", "TERMINATED", [283, 356, 279, 280, 336, 282, 281, 286, 285, 358, 295, 288, 332, 327, 264, 247, 337, 313, 287, 335, 341, 284, 339, 338, 352, 350, 351, 340, 246, 244, 242, 245, 243, 241, 324, 348, 357, 319, 364, 297, 333, 342, 349, 362, 355, 359, 311, 345, 300, 251, 322, 346, 249, 302, 274, 248, 303, 344, 250, 343, 254, 289, 328, 294, 293, 252, 329, 292, 347, 363, 299, 305, 235, 325, 360, 315, 321, 310, 318, 361, 290, 255, 232, 323, 291, 326, 265, 301, 253, 261, 304, 259, 296, 260, 257, 267, 266, 256, 298, 258, 236, 272, 231, 314, 307, 233, 312, 239, 276, 273, 238, 316, 237, 308, 309, 230, 275, 317, 234, 320, 334], [3]]}) (found 0 results, but was looking for 2)):
  app/controllers/topologies_controller.rb:38:in `index'

log in rails 3.2.2

  Location Load (0.4ms)  SELECT "location".* FROM "location"
  Location Load (0.9ms)  SELECT "location".* FROM "location" WHERE "location"."name" = 'Shanghai' LIMIT 1
  Testbed Load (1.3ms)  SELECT "testbed".* FROM "testbed" WHERE "testbed"."tb_type" = 'OSEXEC'
Topology Load (15.3ms)  SELECT "topologies".* FROM "topologies" WHERE (status != 'TERMINATED' AND testbed_id IN (283,356,279,280,336,282,281,286,285,358,295,288,332,327,264,247,337,313,287,335,341,284,339,338,352,350,351,340,246,244,242,245,243,241,324,348,357,319,364,297,333,342,349,362,355,359,311,345,300,251,322,346,249,302,274,248,303,344,250,343,254,289,328,294,293,252,329,292,347,363,299,305,235,325,360,315,321,310,318,361,290,255,232,323,291,326,265,301,253,261,304,259,296,260,257,267,266,256,298,258,236,272,231,314,307,233,312,239,276,273,238,316,237,308,309,230,275,317,234,320,334) AND location_id IN (3))

看了下 3.2 的 log 才是想要的解析行为。这里 4.2.5log 解析出来的结果不对,百思不得其解啊。这是为啥?

看起来你这个 Topology.find(:all, conditions: xxx)用法被淘汰了,建议换成 Topology.where试试。

def find(*ids) # :nodoc:
        # We don't have cache keys for this stuff yet
        return super unless ids.length == 1

   ....

Finder 语法淘汰了。来自 Rails 1.x 的搜索语法。

#3 楼 @msg7086 谢谢。rails guides 里找到了。看书还是太不认真。Rails 4.0 has deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do. For example, Book.find(:all, conditions: { name: '1984' }) has been deprecated in favor of Book.where(name: '1984')

#4 楼 @xiaoping_rubyist 一般来说从 Rails 3 开始玩起的都不会知道 Finder 语法。我 Rails 4 写了一年多了,到了新公司一票的 Rails 1 才知道 Finder 这东西。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号