gpt4 book ai didi

ruby-on-rails - Rails 4 has_and_belongs_to_many 不能正常使用包含语句

转载 作者:行者123 更新时间:2023-11-29 11:20:03 30 4
gpt4 key购买 nike

最近我遇到了 Rails 4 和 HABTM 关系的神秘错误。首先是我的 Gemfile:

source 'https://rubygems.org'
gem 'rails', '~> 4.1.6'
gem 'pg'

下一步。我的模型:

class User < ActiveRecord::Base
end

class Teacher < User
has_and_belongs_to_many :resources, foreign_key: :user_id
end

class Resource < ActiveRecord::Base
has_and_belongs_to_many :teachers, association_foreign_key: :user_id
end

原始数据库数据:

select * from resources;
id | created_at | updated_at
----+----------------------------+----------------------------
1 | 2014-10-13 08:24:07.308361 | 2014-10-13 08:24:07.308361
2 | 2014-10-13 08:24:07.889907 | 2014-10-13 08:24:08.156898
3 | 2014-10-13 08:24:08.68579 | 2014-10-13 08:24:08.884731
4 | 2014-10-13 08:24:09.997244 | 2014-10-13 08:24:10.205753
(4 rows)

select * from users;
id | created_at | updated_at | type
----+----------------------------+----------------------------+---------
13 | 2014-10-13 08:24:01.086192 | 2014-10-13 08:24:01.086192 | Teacher
12 | 2014-10-13 08:24:00.984957 | 2014-10-13 08:24:00.984957 | Teacher
2 | 2014-10-13 08:23:59.950349 | 2014-10-16 08:46:02.531245 | Teacher
(3 rows)

select * from resources_users;
user_id | resource_id
---------+-------------
13 | 1
2 | 2
12 | 3
2 | 4
(4 rows)

最后是错误:

➜  rails_test  bundle exec rails c
Loading development environment (Rails 4.1.6)
2.1.2 :001 > Resource.all.includes(:teachers).map(&:teachers).map(&:to_a)
Resource Load (0.6ms) SELECT "resources".* FROM "resources"
SQL (1.3ms) SELECT "resources_users".*, "resources_users"."user_id" AS t0_r0, "resources_users"."resource_id" AS t0_r1, "users"."id" AS t1_r0, "users"."created_at" AS t1_r1, "users"."updated_at" AS t1_r2, "users"."type" AS t1_r3 FROM "resources_users" LEFT OUTER JOIN "users" ON "users"."id" = "resources_users"."user_id" AND "users"."type" IN ('Teacher') WHERE "users"."type" IN ('Teacher') AND "resources_users"."resource_id" IN (1, 2, 3, 4)
=> [
[#<Teacher id: 13, created_at: "2014-10-13 08:24:01", updated_at: "2014-10-13 08:24:01", type: "Teacher">],
[],
[],
[]]

如您所见,集合中仅返回第一组教师。但是 Rails 生成的 SQL 是正确的并返回所有数据:

SELECT "resources_users".*, "resources_users"."user_id" AS t0_r0, "resources_users"."resource_id" AS t0_r1, "users"."id" AS t1_r0, "users"."created_at" AS t1_r1, "users"."updated_at" AS t1_r2, "users"."type" AS t1_r3 FROM "resources_users" LEFT OUTER JOIN "users" ON "users"."id" = "resources_users"."user_id" AND "users"."type" IN ('Teacher') WHERE "users"."type" IN ('Teacher') AND "resources_users"."resource_id" IN (1, 2, 3, 4);
user_id | resource_id | t0_r0 | t0_r1 | t1_r0 | t1_r1 | t1_r2 | t1_r3
---------+-------------+-------+-------+-------+----------------------------+----------------------------+---------
13 | 1 | 13 | 1 | 13 | 2014-10-13 08:24:01.086192 | 2014-10-13 08:24:01.086192 | Teacher
2 | 2 | 2 | 2 | 2 | 2014-10-13 08:23:59.950349 | 2014-10-16 08:46:02.531245 | Teacher
12 | 3 | 12 | 3 | 12 | 2014-10-13 08:24:00.984957 | 2014-10-13 08:24:00.984957 | Teacher
2 | 4 | 2 | 4 | 2 | 2014-10-13 08:23:59.950349 | 2014-10-16 08:46:02.531245 | Teacher
(4 rows)

有没有人遇到过这样的问题?我不明白这里发生了什么。

附言如果你这样做 Resource.all.includes(:teachers).map { |r| r.reload.teachers 结果正确。然而,它完全消除了 include 的意义,并提供了 N+1 问题。

更新:还有一个值得一提的发现。如果我删除 STI,一切正常。

最佳答案

我重新创建了那些 ActiveRecord Rails 4.1.6 中的模型和数据库记录 pg gem 并看到了正确的行为:


irb(main):017:0> Resource.all.includes(:teachers).map(&:teachers).map(&:to_a)
Resource Load (0.6ms) SELECT "resources".* FROM "resources"
SQL (6.9ms) SELECT "resources_users".*, "resources_users"."id" AS t0_r0, "resources_users"."resource_id" AS t0_r1, "resources_users"."user_id" AS t0_r2, "users"."id" AS t1_r0, "users"."type" AS t1_r1, "users"."created_at" AS t1_r2, "users"."updated_at" AS t1_r3 FROM "resources_users" LEFT OUTER JOIN "users" ON "users"."id" = "resources_users"."user_id" AND "users"."type" IN ('Teacher') WHERE "users"."type" IN ('Teacher') AND "resources_users"."resource_id" IN (1, 2, 3, 4)
=> [[#<Teacher id: 13, type: "Teacher", created_at: "2015-11-05 07:02:59", updated_at: "2015-11-05 07:02:59">], [#<Teacher id: 2, type: "Teacher", created_at: "2015-11-05 07:02:20", updated_at: "2015-11-05 07:02:32">], [#<Teacher id: 12, type: "Teacher", created_at: "2015-11-05 07:03:50", updated_at: "2015-11-05 07:03:50">], [#<Teacher id: 2, type: "Teacher", created_at: "2015-11-05 07:02:20", updated_at: "2015-11-05 07:02:32">]]

关于ruby-on-rails - Rails 4 has_and_belongs_to_many 不能正常使用包含语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26507074/

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