如何从 Has Many Through 中获取附加属性

发布于 2024-09-09 00:41:11 字数 1189 浏览 4 评论 0原文

我正在使用Rails 3 beta 4。

我有以下模型:

class Player < ActiveRecord::Base
has_many :players_items, :dependent => :destroy 
has_many  :items, :through => :players_items
end

class PlayersItem < ActiveRecord::Base
  belongs_to :player 
  belongs_to :item  
end

class Item < ActiveRecord::Base
  has_many :players_items, :dependent => :destroy
  has_many :players, :through =>  :players_items
end

在players_controller中,

def items
    @player = Player.find(params[:id])
    @player_items = @player.items
  end

我有以下属性

--Items Model--
Item_id:Integer
Name:String
Cost:Integer
Description:Text

--PlayersItem Model--
Item_id:Integer
Player_id:Integer
Total:Integer
Traded:Integer

我试图打印出与玩家关联的所有项目,并为每个项目打印出“名称”,“成本”, “描述”、“总计”和“交易”值。

当我在 items.html.erb 中调用 @player_items 时,我只能访问与 Item Model 关联的属性,而不能访问与 PlayersItem 模型关联的任何属性。

我正在尝试在类似于 SQL Join 语句的同一“调用”中访问 items 模型和players_items 模型中的属性,这样

SELECT * FROM players_items INNER JOIN items ON players_items.item_id=items.id  
WHERE players_items.player_id = "@player"

可能吗?

I am using Rails 3 beta 4.

I have the following models:

class Player < ActiveRecord::Base
has_many :players_items, :dependent => :destroy 
has_many  :items, :through => :players_items
end

class PlayersItem < ActiveRecord::Base
  belongs_to :player 
  belongs_to :item  
end

class Item < ActiveRecord::Base
  has_many :players_items, :dependent => :destroy
  has_many :players, :through =>  :players_items
end

In the players_controller

def items
    @player = Player.find(params[:id])
    @player_items = @player.items
  end

I have the following attributes

--Items Model--
Item_id:Integer
Name:String
Cost:Integer
Description:Text

--PlayersItem Model--
Item_id:Integer
Player_id:Integer
Total:Integer
Traded:Integer

I am trying to print out all the items associated with a player and for each item print out the "Name", "Cost", "Description", "Total", and "Traded" values.

When I call @player_items in the items.html.erb, I can only access the attributes associated with the Item Model and not any of the attributes associated with PlayersItem model.

I am trying to access the attributes from both the items model and players_items model in the same "call" similar to SQL Join Statement like this

SELECT * FROM players_items INNER JOIN items ON players_items.item_id=items.id  
WHERE players_items.player_id = "@player"

Is this possible?

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

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

发布评论

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

评论(2

旧情勿念 2024-09-16 00:41:11
@player = Player.order("created_at").last
@player.players_items.each do |item|
  puts "#{item.player}: #{item.description} cost:#{item.cost}"
end

有很多通过是有点奇怪。将其视为一个模型,其名称(理想情况下)应该描述其他两个模型之间的关系。因此,如果装备被分配给玩家,你可以称之为加入模型分配或贷款或其他什么。 Players_items 是连接表的命名约定,不会直接寻址。
我希望这有帮助!

@player = Player.order("created_at").last
@player.players_items.each do |item|
  puts "#{item.player}: #{item.description} cost:#{item.cost}"
end

Has many through is a little weird. Think of it as a model whose name should (ideally) be descriptive of the relationship between the two other models. So maybe if equipment is being distributed to the players you could call the join model distributions or loans or something. The players_items is the naming convention for join tables which aren't going to be addressed directly.
I hope that helps!

绝不服输 2024-09-16 00:41:11

用在控制器

@player = Player.find(params[:id], :include => [:items,:players_items])

和视图中

@player.players_items.each do |player| 
puts "#{player.name}: #{player.player.item.description} cost:#{player.item.cost}"
end

Used in the controller

@player = Player.find(params[:id], :include => [:items,:players_items])

And in the view

@player.players_items.each do |player| 
puts "#{player.name}: #{player.player.item.description} cost:#{player.item.cost}"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文