ActiveRecord Validations (noob question)

Hey everyone, I’m just beginning rails (and this list) and I am
having a little trouble with something. I’m trying to make sure I
validate everything that needs to be validated, but I’m having a hard
time grasping how ActiveRecord handles validations. I have a schema
like this:

Account

  • has_many Users
  • has_one ServicePlan

I’ve included my code sample at the bottom of the email. Basically I
can do a ‘validates_presence_of :service_plan’ but I can’t figure out
how to ‘validates_numericality_of :service_plan’ or
‘validates_numericality_of :service_plan_id’ or anything. I BELIEVE
I understand that I should never have to access
Account.service_plan_id directly since I would always use
Account.service_plan.id, so is it correct that I dont have to
validate the type of the service plan Id? I just “trust” rails?

Also, as a side question, how does rails translate class names into
property names? I don’t quite grasp the naming convention. Is the
above correct in that the ServicePlan belonging to an Account object
is ‘MyAccount.service_plan’ and not ‘MyAccount.servicePlan’?

Sorry for the beginner questions. I guess I gotta start somewhere
right?

Code Sample:

class Account < ActiveRecord::Base
has_many :users
has_one :service_plan
validates_presence_of :name, :service_plan
validates_numericality_of :service_plan # ??? Does this need to
be validated?
end

Thanks,
Cliff

On Jul 7, 9:48 pm, Cliff P. [email protected] wrote:

I’ve included my code sample at the bottom of the email. Basically I
can do a ‘validates_presence_of :service_plan’ but I can’t figure out
how to ‘validates_numericality_of :service_plan’ or
‘validates_numericality_of :service_plan_id’ or anything. I BELIEVE
I understand that I should never have to access
Account.service_plan_id directly since I would always use
Account.service_plan.id, so is it correct that I dont have to
validate the type of the service plan Id? I just “trust” rails?

The ID column is always an auto-incementing integer, so you don’t have
to validate it.

Also, as a side question, how does rails translate class names into
property names? I don’t quite grasp the naming convention. Is the
above correct in that the ServicePlan belonging to an Account object
is ‘MyAccount.service_plan’ and not ‘MyAccount.servicePlan’?

Right. Ruby convention is all lower case, with underscores between
English words to make it more readable (as opposed to, say, Java,
which capitalizes the first letter of words after the first word).

Sorry for the beginner questions. I guess I gotta start somewhere
right?

Don’t apologize, that’s what this list is for!

Jeff