Forum: Ruby on Rails ActiveRecord Validations (noob question)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E61b179b61fcd0441fa74a99a26ed472?d=identicon&s=25 Cliff Pruitt (Guest)
on 2007-07-08 04:49
(Received via mailing list)
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
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2007-07-08 05:28
(Received via mailing list)
On Jul 7, 9:48 pm, Cliff Pruitt <lists.cpru...@cliffpruitt.com> 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
www.softiesonrails.com
This topic is locked and can not be replied to.