Rails4弃用警告

时间:2014-03-17 11:12:25

标签: ruby-on-rails-4

当我从rails 3.2升级到rails 4.0时,Rails4正在收到折旧警告。我有这个问题。

Child.find(:all, :include => :children_users, :conditions => "state = 'active' AND owner_id = #{self.id} AND children_users.user_id = #{other_user.id}")

我收到弃用警告如下: -

DEPRECATION WARNING: It looks like you are eager loading table(s) (one of: splits, accounts) that are referenced in a string SQL snippet. For example:

    Post.includes(:comments).where("comments.title = 'foo'")

Currently, Active Record recognizes the table in the string, and knows to JOIN the comments table to the query, rather than loading comments in a separate query. However, doing this without writing a full-blown SQL parser is inherently flawed. Since we don't want to write an SQL parser, we are removing this functionality. From now on, you must explicitly tell Active Record when you are referencing a table from a string:

    Post.includes(:comments).where("comments.title = 'foo'").references(:comments)

If you don't rely on implicit join references you can disable the feature entirely by setting `config.active_record.disable_implicit_join_references = true`. (called from splits_under_100_percent at /Users/newimac/RailsApp/bank/app/models/user.rb:274)

要解决这个问题,我试试这个

Child.includes(:children_users).where(state: active, owner_id: self.id, children_users.user_id = other_user.id).load

Child.where{(state: active, owner_id: self.id, children_users.user_id = other_user.id).includes(:children_users)}

但它们都不起作用。

2 个答案:

答案 0 :(得分:1)

children_users.user_id = other_user.id错误。

正确的是:"children_users.user_id" => other_user.id

答案 1 :(得分:0)

感谢@Zakwan。最后,这个查询有效。

Child.includes(:children_users).where(state: 'active', owner_id: self.id, "children_users.user_id" => other_user.id).load