Anyone able to help with some code for aliasing legacy database columns? I''ve got a hideous old schema to work with. I''m looking for something that would allow something like: class Project < ActiveRecord::Base ... alias_field ‘admin_project_name’=>’name’ ... end I looked at: http://www.robbyonrails.com:8680/articles/2005/07/25/the-legacy-of-databases- with-rails but couldn''t get access to Rick Olson''s latter suggestion. Any help / suggestions much appreciated Graham.
you could write accessor methods to wrap the culumn names, something like def name=(value) self.admin_project_name = value end def name self.admin_project_name end or some such. GArrow wrote:>Anyone able to help with some code for aliasing legacy database columns? I''ve >got a hideous old schema to work with. > >I''m looking for something that would allow something like: > >class Project < ActiveRecord::Base > ... > alias_field ‘admin_project_name’=>’name’ > ... >end > >I looked at: >http://www.robbyonrails.com:8680/articles/2005/07/25/the-legacy-of-databases- >with-rails > >but couldn''t get access to Rick Olson''s latter suggestion. > >Any help / suggestions much appreciated > >Graham. > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Francois Paul <francois@...> writes:> > you could write accessor methods to wrap the culumn names, something like > > def name=(value) > self.admin_project_name = value > end > def name > self.admin_project_name > end > > or some such. >Thanks Francois I''ve got around 20 fields on a couple of tables that need tidying up and it''s a bit of a cludge. I wondered if there was a class decorator that could do this, as I''m sure someone must have figured a way to do it. (Not literate enough in ruby/rails to do it myself)
Chris McGrath suggested this method, to be required: module ActiveRecord class ActiveRecord::Base def alias_column(*options) if options.kind_of? Hash options.each do |old_name, new_name| self.send(:define_method, new_name) { self.old_name } self.send(:define_method, "#{new_name}=") { |value| self.old_name = value } end end end end Then in your model: alias_column ''crappy_Name'' => ''name'' Joshua Sierles On 8/5/05, GArrow <g.arrowsmith-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Francois Paul <francois@...> writes: > > > > you could write accessor methods to wrap the culumn names, something > like > > > > def name=(value) > > self.admin_project_name = value > > end > > def name > > self.admin_project_name > > end > > > > or some such. > > > > Thanks Francois > > I''ve got around 20 fields on a couple of tables that need tidying up and > it''s a > bit of a cludge. I wondered if there was a class decorator that could do > this, > as I''m sure someone must have figured a way to do it. (Not literate enough > in > ruby/rails to do it myself) > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 8/5/05, Joshua Sierles <jsierles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Chris McGrath suggested this method, to be required: ><snipped> This would be nice as a patch to ActiveRecord itself. Chris
I agree. This would provide a nice boost to the ease of use when legacy dbs are concerned. On Aug 5, 2005, at 8:35 AM, Chris McGrath wrote:> On 8/5/05, Joshua Sierles <jsierles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Chris McGrath suggested this method, to be required: >> >> > <snipped> > > This would be nice as a patch to ActiveRecord itself. > > Chris > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Joshua Sierles <jsierles@...> writes:> > > Chris McGrath suggested this method, to be required:module ActiveRecord classActiveRecord::Base def alias_column(*options) if options.kind_of? Hash options.each do |old_name, new_name|> self.send(:define_method, new_name) { self.old_name }self.send(:define_method, "#{new_name}=") { |value| self.old_name = value } end end endendThen in your model:> alias_column ''crappy_Name'' => ''name''Joshua Sierles >Brilliant work. Thanks to you both. Is is just me or is there a missing ''end''? Chris -> will you request a patch?
The previous code should be: module Legacy def self.append_features(base) super base.extend(ClassMethods) end module ClassMethods def alias_column(options) options.each do |old_name, new_name| self.send(:define_method, new_name) { self.send(old_name) } self.send(:define_method, "#{new_name}=") { |value| self.send(old_name) = value } end end end end ActiveRecord::Base.class_eval do include Legacy end is there a better way to do this? Joshua _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
And in your model: alias_column ''loc_desc'' => ''name'' On 8/5/05, Joshua Sierles <jsierles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > The previous code should be: > > module Legacy > def self.append_features(base) > > super > base.extend(ClassMethods) > end > module ClassMethods > > def alias_column(options) > options.each do |old_name, new_name| > self.send(:define_method, new_name) { self.send(old_name) } > > self.send(:define_method, "#{new_name}=") { |value| self.send(old_name) = value } > > end > end > end > end > > ActiveRecord::Base.class_eval do > include Legacy > > end > > > > is there a better way to do this? > > Joshua >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
The final working code for this is here: http://www.bigbold.com/snippets/posts/show/556 On 8/5/05, Joshua Sierles <jsierles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > And in your model: > > alias_column ''loc_desc'' => ''name'' > > On 8/5/05, Joshua Sierles <jsierles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > The previous code should be: > > > > module Legacy > > > > def self.append_features(base) > > > > super > > base.extend(ClassMethods) > > end > > module ClassMethods > > > > def alias_column(options) > > options.each do |old_name, new_name| > > self.send(:define_method, new_name) { self.send(old_name) } > > > > > > self.send(:define_method, "#{new_name}=") { |value| self.send(old_name) = value } > > > > end > > end > > end > > end > > > > ActiveRecord::Base.class_eval do > > include Legacy > > > > end > > > > > > > > is there a better way to do this? > > > > Joshua > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails