Rails3 和 ActiveRecord 与旧数据库:JOIN 不返回其他表列

发布于 2024-12-28 05:32:34 字数 1290 浏览 1 评论 0原文

我有一个旧数据库,其中包含两个不同的表(tbl_players 和 tbl_player_ ratings),它们在公共列(player_key)上相互链接。

我的问题:使用 Rails3,当我尝试检索加入到 Players 的 PlayersRatings 时,仅返回 PlayerRatings 中的列。但是,如果我在 mysql 命令行执行相同的 SQL 查询,则会返回两个表中的列。

为简单起见,我在这里仅提供每个表中的几列。

tbl_players

player_key, last_name

tbl_player_ ratings

player_key, rating

表示这些表的我的Rails 类如下所示:

class PlayerRating < ActiveRecord::Base
  establish_connection :legacy
  set_table_name 'tbl_player_ratings'
  set_primary_key "player_key"
  belongs_to :player,
             :foreign_key => 'player_key'
end

class Player < ActiveRecord::Base
  establish_connection :legacy
  set_table_name 'tbl_players'
  set_primary_key "player_key"
  has_many :player_ratings,
           :foreign_key => 'player_key'
end

我在rails 控制台中运行的查询:

PlayerRating.joins(:player).select("*").limit(1)

这将返回一个单独的PlayerRating,而不表示任何Player 字段。

上述rails命令生成的SQL:

SELECT * FROM `tbl_player_ratings` INNER JOIN `tbl_players` ON `tbl_players`.`player_key` = `tbl_player_ratings`.`player_key` LIMIT 1

当我在mysql命令行执行该命令时,两个表中的所有列都会返回。

为什么 Rails 和 ActiveRecord 不做同样的事情(返回两个表中的所有列)?

I have a legacy database that contains two different tables (tbl_players and tbl_player_ratings) that link to one another on a common column (player_key).

My problem: With Rails3, when I attempt to retrieve PlayersRatings joined to Players, only columns from PlayerRatings are returned. However, if I execute the same SQL query at the mysql command line, columns from both tables are returned.

For simplicity, I here provide just a few of the columns from each table.

tbl_players

player_key, last_name

tbl_player_ratings

player_key, rating

My Rails classes representing these tables look as follows:

class PlayerRating < ActiveRecord::Base
  establish_connection :legacy
  set_table_name 'tbl_player_ratings'
  set_primary_key "player_key"
  belongs_to :player,
             :foreign_key => 'player_key'
end

class Player < ActiveRecord::Base
  establish_connection :legacy
  set_table_name 'tbl_players'
  set_primary_key "player_key"
  has_many :player_ratings,
           :foreign_key => 'player_key'
end

The query that I'm running in the rails console:

PlayerRating.joins(:player).select("*").limit(1)

This returns a sole PlayerRating without any Player fields represented.

The SQL produced by the above rails command:

SELECT * FROM `tbl_player_ratings` INNER JOIN `tbl_players` ON `tbl_players`.`player_key` = `tbl_player_ratings`.`player_key` LIMIT 1

When I execute that exact command at the mysql command-line, all columns in both tables are returned.

Why does Rails and ActiveRecord not do the same (return all columns in both tables)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2025-01-04 05:32:34

PlayerRating 只是一个 PlayerRating 对象;您不能通过查询上的 joins(:player) 强制它成为 PlayerRating-Player 组合。

在该查询之后,您将可以通过 player_ rating.player 访问播放器。

array_of_player_ratings = PlayerRating.joins(:player).limit(any_number) 
single_player_rating = PlayerRating.joins(:player).first 
player = single_player_rating.player

请查看链接的重复问题以获取更完整的描述SQL 查询和 ActiveRecord 查询之间的区别。

A PlayerRating is only a PlayerRating object; you cannot coerce it into being a combination PlayerRating-Player via a joins(:player) on the query.

Following that query, you will have access to the player via player_rating.player.

array_of_player_ratings = PlayerRating.joins(:player).limit(any_number) 
single_player_rating = PlayerRating.joins(:player).first 
player = single_player_rating.player

Do look at the duplicate question linked for a fuller description of the difference between SQL queries and ActiveRecord queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文