Saturday, April 9, 2011

Model misbehavior

I've been trying to get a handle on models and associating them with other models this weekend. Here's what's going on:

I'm working on Dungeon Roller and I've created two models: Character and Statistic. What I want to do is have Statistic display under Character. So logically I need to associate these models such that Character has_one Statistic model and Statistic belongs_to Character, right? These associations (has_one, has_many, belongs_to and has_and_belongs_to_many) all refer to how many of those models can display the model they're associated with, right?

If I can get this relationship figured out, and make it so that Statistic will display on Character and remain separately editable, I can use that as a template to hammer out other models I've got planned. I'll eventually have 7 or 8 models that belong to the main Character model. I ran into some trouble earlier when I named Character as Characters, not realizing that is not the way to do things. I also ran into some trouble with reserved keywords. I had "class" as a string in the Character model and eventually figured out I couldn't use it.

So while today's been frustrating because I haven't made as much progress as I'd like to, I got those two things figured out at least.

2 comments:

  1. Naming is a bit tricky at first but it seems like you're getting the hang of it.

    I think your strategy of getting statistics nailed down, and then expanding into other associations is a great approach.

    ReplyDelete
  2. @Eli Yeah, I agree. It doesn't make sense to wrangle four classes at once when I can just get one figured out then use it as a template to get the other three working.

    ReplyDelete