Monday, May 2, 2011

Mo models, mo problems

So I've made some more progress with Manticore, thanks to the help of the internet at large and my friend Eli. I've finally gotten the problem of each character sharing the same set of statistics straightened out. I restarted the project and kept the old code so I could compare it. This time around I'm using partials to render and display the statistics in the character view and it's keeping everything much tidier.

Keeping my old code was a good idea, because I'm still referencing it. So far, I'm able to create a new character, then create statistics for each character. However, I'm still having a couple of problems. Each character can still have multiple statistics and I don't have a way to simply update that characters' statistics. Tomorrow I'm going to look through the old code because the first time around I generated a scaffold for statistics and it generated a method for editing.

I've been a bit discouraged because I didn't make much progress this weekend. And in the application I'm building with Agile Web Development, I hit a wall because I edited the code to force users to log in before proceeding without creating a user first. So now I can't view the changes I've made until I either create a user somehow or undo the last dozen or so edits. Needless to say, I've been discouraged from working any further on this project.

Is there a way to simply add a user to a database? Maybe with rails console? I might look into that, but for now I'm focused on working on Manticore again, especially since I'm making some progress.

A few more questions. I'm still unclear on has_one vs. has_many. For example, I have this code for the character model:

class Character < ActiveRecord::Base   validates :name, :presence => true
  validates :profession, :presence => true
  validates :level, :presence => true

  has_many :statistics

Is has_many the correct association to make here? The statistic model is a table with 6 separate stats. So does it count as one table, or six individual elements? has_many is working, but I thought if I wrote it as has_one, I might avoid the problem of having multiple instances of the statistic model.

And how does sqlite3 handle foreign keys? I'm still unclear on exactly WHAT a foreign key is. Here's my guess:

class StatisticsController < ApplicationController
  def create
    @character = Character.find(params[:character_id])
    @statistic = @character.statistics.create(params[:statistic])
    redirect_to character_path(@character)

In this code from the statistics controller, I'm assuming the foreign key is @character in the @character.statistics.create declaration?

I kind of feel like some very basic stuff is giving me trouble, but I'm still learning and having fun, so onward!