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! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 26, 6:20 am, Rick F <n...-gqmXzWjnWO954TAoqtyWWQ@public.gmane.org> wrote:> 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> > Please keep in mind that I''ve only been playing with Rails for about 2 > hours, so I''m still learning.. > > Thanks!-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, 26 Jul 2011 05:37:42 -0700 (PDT), Frederick Cheung wrote:> On Jul 26, 6:20 am, Rick F <n...-gqmXzWjnWO954TAoqtyWWQ@public.gmane.org> wrote:[ 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! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 26, 5:06 pm, Rick & Nellie Flower <n...-gqmXzWjnWO954TAoqtyWWQ@public.gmane.org> wrote:> > 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 ( http://guides.rubyonrails.org/association_basics.html ) Fred> > 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!-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, 26 Jul 2011 09:11:53 -0700 (PDT), Frederick Cheung wrote:> On Jul 26, 5:06 pm, Rick & Nellie Flower <n...-gqmXzWjnWO954TAoqtyWWQ@public.gmane.org> wrote: >> > 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 ( > http://guides.rubyonrails.org/association_basics.html )Thanks! I''ll check it out! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.