Model Associations, multiple models in Rails
I have some models named GameItem, InventoryItem, Inventory, User, Bonuse an开发者_如何学运维d Bonuses_Game_Items
A User has one inventory.
A user has many inventory items through inventory. A Game Item has and belongs to many bonuses through Bonuses_Game_Items. An Inventory has many InventoryItems. An InventoryItem is like (inventory_id, game_item_id).Simply, a game item is the static representation of the item in the db, while the inventory item is the combination of an item and a user's inventory. The question now is :
how can i get the bonuses out of an inventory_item ? what are the associations that i have to use ? I can't get it right.
Hei there,
I am not sure if I understand correctly but I will try to help you, as I had a similar problem (as it seems).
First do I understand you right that InventoryItems belongs to GameItems? Is that relation defined in your models?
If so I believe something like this
@inventoryitem.game_item.bonuses
Have a look here Rails API ActiveRecord::Associations if you have defined the relations in the models then you should be able to use the line above.
That is if your models look somewhat like
InventoryItemModel
belongs_to => :game_items
I hope this helps. Could not test as I have no Rails running on this machine.
Cheers
精彩评论