Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It sounds like you're misunderstanding some of the concepts around models. For example, <code>HitPoints</code> and <code>ArmorClass</code> should not be models that have a <code>has_one</code> association with your <code>Character</code>. They are just attributes of <code>Character</code>. So your model should look something like this instead:</p> <pre><code>class Character attr_accessor :name, :hit_points, :armor_class def initialize(name, hitpoints=10, armor_class=0) @name = name @hitpoints = hitpoints @armor_class = armor_class end # some other stuff, etc end </code></pre> <p>Then you can access them like so:</p> <pre><code>fighter = Character.new("Conan", 100, 12) puts "I am #{fighter.name} and I have #{fighter.hit_points} hitpoints." puts fighter.armor_class # outputs 12 </code></pre> <p>Also, I'd say <code>Equipment</code> model is the wrong concept. What you need is an <code>Item</code> model. All of the things you mentioned (weapons, armor, gear) are all just items. And it makes a lot more sense that a "Character" <code>has_many</code> "Items". Also that "Item" <code>has_many</code> "Characters" since you'll presumably have multiple characters with multiple items...so it COULD be a many to many relationship to keep it simple. Then your "Item" model could have an "ItemType" field which tells you if it's a weapon, armor, potion, etc.</p> <p>So your <code>Item</code> model could look like this</p> <pre><code>class Item attr_accessor :name, :type def initialize(name, type) @name = name @type = type end end </code></pre> <p>Another option, which is closer to what you're talking about is to add an <code>ItemSet</code> model. We'd put this model between <code>Character</code> and <code>Items</code>. Doing it this way would let you say</p> <ul> <li>"Character" <code>has_one</code> "ItemSet"</li> <li>"ItemSet" <code>has_many</code> "Items"</li> <li>Character" has many "Items" through "ItemSet"</li> </ul> <p>So basically, this is showing that in your "world" you have many characters and items. But each character has a unique set of those items.</p> <p>This is the way I'd do it.</p> <p>In the code above you need to add the <code>has_many</code>, <code>has_one</code>, etc attributes to the models. I left it out so you could see the models themselves a little easier and so you could figure out how you want to do it yourself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload