I have a an STI table which acts_as_tree, that has a large number of classes/types. My common fields are: id parent_id name description with 4 more text fields, I could cover most of my classes if I could redefine the name of the field like this. text1 AS address1 text2 AS address2 text3 AS zipcode For one class and text 1 AS model_number text2 AS vendor Is there a construct that will allow me to do this? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060326/62487e3a/attachment.html
On Sun, Mar 26, 2006 at 07:48:33AM -0500, Jim Schmidt wrote: } I have a an STI table which acts_as_tree, that has a large number of } classes/types. My common fields are: } } id } parent_id } name } description } } with 4 more text fields, I could cover most of my classes if I could } redefine the name of the field like this. } } text1 AS address1 } text2 AS address2 } text3 AS zipcode } } For one class and } } text 1 AS model_number } text2 AS vendor } } } Is there a construct that will allow me to do this? I just wrote something to do that for similar reasons: class Super < ActiveRecord::Base def self.alias_field(old_field, new_field) define_method(new_field) { self.send(old_field) } define_method("#{new_field}=") { |arg| self.send("#{old_field}=", arg) } define_method("#{new_field}?") { self.send("#{old_field}?") } end end class Sub < Super alias_field :text1, :address1 alias_field :text2, :address2 alias_field :text3, :zipcode end Now I can do all of the following: fail "Where''s the address?" unless foo.address1? foo.address1 = ''1600 Pennsylvania Ave.'' addr = foo.address1 ...and it will use text1?, text1=, and text1, respectively. } Thanks --Greg
Thanks, that is exactly what I need. On 3/26/06, Gregory Seidman <gsslist+ror@anthropohedron.net> wrote:> > On Sun, Mar 26, 2006 at 07:48:33AM -0500, Jim Schmidt wrote: > } I have a an STI table which acts_as_tree, that has a large number of > } classes/types. My common fields are: > } > } id > } parent_id > } name > } description > } > } with 4 more text fields, I could cover most of my classes if I could > } redefine the name of the field like this. > } > } text1 AS address1 > } text2 AS address2 > } text3 AS zipcode > } > } For one class and > } > } text 1 AS model_number > } text2 AS vendor > } > } > } Is there a construct that will allow me to do this? > > I just wrote something to do that for similar reasons: > > class Super < ActiveRecord::Base > > def self.alias_field(old_field, new_field) > define_method(new_field) { self.send(old_field) } > define_method("#{new_field}=") { |arg| self.send("#{old_field}=", arg) > } > define_method("#{new_field}?") { self.send("#{old_field}?") } > end > > end > > class Sub < Super > alias_field :text1, :address1 > alias_field :text2, :address2 > alias_field :text3, :zipcode > end > > Now I can do all of the following: > > fail "Where''s the address?" unless foo.address1? > foo.address1 = ''1600 Pennsylvania Ave.'' > addr = foo.address1 > > ...and it will use text1?, text1=, and text1, respectively. > > } Thanks > --Greg > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060326/62c93511/attachment.html
Maybe Matching Threads
- transforming a badly organized data base into a list of data frames
- regular expressions
- struggling with "split" function
- Searching for Enumerated Items using str_count() from the stringr package
- Searching for Enumerated Items using str_count() from the stringr package