如何从 Has Many Through 中获取附加属性
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多通过是有点奇怪。将其视为一个模型,其名称(理想情况下)应该描述其他两个模型之间的关系。因此,如果装备被分配给玩家,你可以称之为加入模型分配或贷款或其他什么。 Players_items 是连接表的命名约定,不会直接寻址。
我希望这有帮助!
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!
用在控制器
和视图中
Used in the controller
And in the view