Newbie question..

Hi all.. Just trying out Rails for the first time.. I've got a project I'd like to port over from another environment and am just not sure how to achieve the same sort of OO goodness I'm used to.. In my other environment, I've declared a few classes similar to the following (these are only a few of the overall classes):

class Address :    street : string    city : string    state : string    zip : string    email : string    phone : string

class Name :    lastname : string    husband : string    wife : string    children : array of strings

class User    acctLocked : boolean    address : Address (object)    isProfileSetup : boolean    lastLogin : datetime    name : Name (object)    userType : enum(#regular, #admin)

In this case, my old environment allowed me to create validation logic associated with each class so I didn't have to reproduce (for instance) address validation logic for the various classes (not present here) that had address records -- all was encapsulated within a single class to reduce/eliminate redundant code.

So -- is this sort of compound objecting possible in Rails or should I just eliminate the first two classes and add their respective fields directly into Users?

Also, if I want to set a max field size for some of the strings (for validation purposes or otherwise), what's the best way to do that to ensure my generated SQL limits string length to 2 characters for state (for instance)?

Please keep in mind that I've only been playing with Rails for about 2 hours, so I'm still learning..

Thanks!

Hi all.. Just trying out Rails for the first time.. I've got a project I'd like to port over from another environment and am just not sure how to achieve the same sort of OO goodness I'm used to.. In my other environment, I've declared a few classes similar to the following (these are only a few of the overall classes):

class Address : street : string city : string state : string zip : string email : string phone : string

class Name : lastname : string husband : string wife : string children : array of strings

class User acctLocked : boolean address : Address (object) isProfileSetup : boolean lastLogin : datetime name : Name (object) userType : enum(#regular, #admin)

In this case, my old environment allowed me to create validation logic associated with each class so I didn't have to reproduce (for instance) address validation logic for the various classes (not present here) that had address records -- all was encapsulated within a single class to reduce/eliminate redundant code.

So -- is this sort of compound objecting possible in Rails or should I just eliminate the first two classes and add their respective fields directly into Users?

Sounds like you want either validates_with, which enables you to package up a set of validations into something reusable or validates_associated, which would tell your user that when it validates itself it should also validate the associated address object.

Also, if I want to set a max field size for some of the strings (for validation purposes or otherwise), what's the best way to do that to ensure my generated SQL limits string length to 2 characters for state (for instance)?

validates_length_of ?

Fred

[ snipped ]

So -- is this sort of compound objecting possible in Rails or should I just eliminate the first two classes and add their respective fields directly into Users?

Sounds like you want either validates_with, which enables you to package up a set of validations into something reusable or validates_associated, which would tell your user that when it validates itself it should also validate the associated address object.

Ok.. I'll check into that.. However, I'm still wondering if I can have nested objects like I mentioned above -- e.g. a User object containing an Address object and when it's time to write to the database, both a User object is written but the associated Address object is also written with some sort of foreign key between the two?

Just want to make sure before I proceed down a dead end path with Rails. Does anyone architect your data that way or just have duplicated data (e.g. an address for a user vs an address record for a vendor vs ??)

Thanks!

> Sounds like you want either validates_with, which enables you to > package up a set of validations into something reusable or > validates_associated, which would tell your user that when it > validates itself it should also validate the associated address > object.

Ok.. I'll check into that.. However, I'm still wondering if I can have nested objects like I mentioned above -- e.g. a User object containing an Address object and when it's time to write to the database, both a User object is written but the associated Address object is also written with some sort of foreign key between the two?

Well contain isn't how I would describe it, but objects that have associations with other objects happens all the time. Checkout the active record associations guide ( Active Record Associations — Ruby on Rails Guides )

Fred

Thanks! I'll check it out!