gpt4 book ai didi

ruby-on-rails - rails ActiveRecord : scope incoherence

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:19 25 4
gpt4 key购买 nike

在我的模型中我有:

#models/friend.rb
scope :approved_friend, where(:approved => true)

Rails 控制台输出:

User.find(2).friends
=> [#<Friend id: 18, user_id: 2, approved: true, created_at: "2013-04-23 09:18:59", updated_at: "2013-04-23 09:18:59", friend_id: 1>]

User.find(2).friends.approved_friend
=> []

注意输出中 approved 为 true ...它变得疯狂的地方在这里:

User.find(1).friends.approved_friend
=> [#<Friend id: 19, user_id: 1, approved: true, created_at: "2013-04-23 09:19:36", updated_at: "2013-04-23 09:19:36", friend_id: 2>]

我错过了什么吗?

编辑:一方面你有这个查询:

SELECT "friends".* FROM "friends" WHERE "friends"."user_id" = 2

=> [#<Friend id: 18, user_id: 2, approved: true, created_at: "2013-04-23 09:18:59", updated_at: "2013-04-23 09:18:59", friend_id: 1>]

另一方面,你有这个(通过范围查询):

SELECT "friends".* FROM "friends" WHERE "friends"."user_id" = 2 AND "friends"."approved" = 't'
=> []

最佳答案

由于 :status 字段在 Friend 模型中,您可能必须将范围更改为此

scope :approved_friend, where('friends.approved' => true).includes(:friend)

关于ruby-on-rails - rails ActiveRecord : scope incoherence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16167221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com