Saturday, April 30, 2011

Trouble with foreign keys

So I've been doing some work on Manticore (previously Dungeon Roller) and I'm stuck. SURPRISE!

Here's the problem: I have two models, character and statistic. Each instance of the character model will have one instance of the statistic model associated with it. I've got the code set up so that I can create new characters with no problem, but all characters are using the same instance of the statistic model. I think this is because I don't have the foreign keys set up correctly and that's largely because I don't know HOW to set up the foreign keys. I know what they are, and for the statistic model I need to use character_id, right? But I don't get how to use it, or how to associate these models so that each character will have one statistic model.

I've spent some time on stack overflow today and got some good feed back, but I'm having trouble finding out how to do exactly what I want. Going over the code I've done through various tutorials isn't helping, either.

Here's what I have for the character model:

class Character < ActiveRecord::Base &nsbp: has_many :statistics, :dependent => :destroy
end

And here's the statistic model:

class Statistic < ActiveRecord::Base
  belongs_to :character
end

My friend Eli's told me I need to associate this as @character.statistic, which makes sense, but I'm still not getting it. Where does this code go? I know the bulk of the logic needs to go in the models, but what about the controllers? I feel fairly confident I can actually display the information in the view once I get it straightened out on the back end. At that point, it'd just be a matter of a link_to or a render tag, right?

Any help is highly appreciated. I've done similar things with the shopping cart application I'm building with Agile Web Development, but I'm having a hard time connecting the dots and extrapolating what I need to do in this case.

Monday, April 25, 2011

That beautiful ArgumentError

Tonight while working on a mailer for the Depot application I'm developing with Agile Web Development with Rails, I screwed my code up and got this error.


I see errors similar to this one pretty often. Most of the time, I've made a typo or forgotten to close a tag or made a call on 'product' instead of '@product'. So I get frustrated when this screen pops up instead of the correct response I was hoping for, but one of the awesome things about Rails is it gives me some guidance on how to fix the error.

In this case, the error claims it's coming from the Orders Controller, but it was really coming from the mailer I created.


What was happening here is these methods aren't referencing anything. So the fix was to write the code like so:


I just skipped over this while working through the book, but if it was MY code, the error information Rails provides would still be valuable. In this case, the error wasn't actually in the Orders Controller, but it still pointed me in the right direction. The screenshot is a bit hard to read at this size (still trying to figure out the best way to display my code) but I'd forgotten to add (order) after defining both order_shipped and order_received.

My hope is I'll see less of these kind of syntax errors as I go along, but I have a feeling typos will be a continual problem.

Sunday, April 24, 2011

Lightening the overload

The longer I work with Ruby on Rails, the more things become clear to me. The more I understand what various acronyms stand for, what they do and how they work. Here's an example, torn from the pages of REAL LIFE:

I work for an e-commerce website, mostly doing SEO work, photography and editing products for the store. The IT manager recently changed our shopping cart because customers were having problems with the Ajax based shopping cart. Before this weekend, I didn't have a clear understanding of what Ajax was. I assumed it was another coding language like Ruby, but it turns out it's just a tool used in web application design. It stands for Asynchronous JavaScript and XML and Agile Web Development had me use it to create an indicator that the shopping cart has been updated in the application I'm working on.

So what have I learned this weekend? I got familiar with Ajax, what it can do and what it's used for (not to mention what it actually IS) and it's given me a better understanding of how everything, from development to design, works together. I first got interested in web design a few years ago, when I fell in love with CSS, and learning Ruby has been nice because I've had to learn how to implement CSS within a Rails application.

When I got started with Ruby a few months ago, I felt like there was a ton of information to process and somehow wrap my head around. That's still true, but the information overload gets lighter every day.