Anybody know of a way to alias a column in ActiveRecord such that you could reference it via a different symbol in your view for view purposes? Like my column is named X but I want to display it as Y and maybe I have an accessor named Y set up on the model so that when I "call" Y from the view (and controller?) I''m actually manipulating the column X? Hope that makes sense. Wes -- Posted via http://www.ruby-forum.com/.
something like this? def y x end def y=(value) x = value end On 3/14/06, Wes Gamble <weyus@att.net> wrote:> Anybody know of a way to alias a column in ActiveRecord such that you > could reference it via a different symbol in your view for view > purposes? > > Like my column is named X but I want to display it as Y and maybe I have > an accessor named Y set up on the model so that when I "call" Y from the > view (and controller?) I''m actually manipulating the column X? > > Hope that makes sense. > > Wes > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Wes Gamble wrote:> Anybody know of a way to alias a column in ActiveRecord such that you > could reference it via a different symbol in your view for view purposes? > > Like my column is named X but I want to display it as Y and maybe I have > an accessor named Y set up on the model so that when I "call" Y from the > view (and controller?) I''m actually manipulating the column X?Use Ruby''s ''alias'' feature. alias :y :x alias :y= :x Make sure you alias after the :x methods are defined, because a later ''def x'' will create a new :x method and :y will point to the old :x. --josh -- Posted via http://www.ruby-forum.com/.
Josh Susser wrote:> Wes Gamble wrote: >> Anybody know of a way to alias a column in ActiveRecord such that you >> could reference it via a different symbol in your view for view purposes? >> >> Like my column is named X but I want to display it as Y and maybe I have >> an accessor named Y set up on the model so that when I "call" Y from the >> view (and controller?) I''m actually manipulating the column X? > > Use Ruby''s ''alias'' feature. > > alias :y :x > alias :y= :x> > Make sure you alias after the :x methods are defined, because a later > ''def x'' will create a new :x method and :y will point to the old :x. > > --joshthis is probably well and all for an experianced programmer to understand. But I''m a total noob, so how would you for instance alias a column name namned "login" to the alias "foo" in a table namned "users" Tried something like this.. and it does not work: class User < ActiveRecord::Base include LoginEngine::AuthenticatedUser belongs_to :account def login foo end def login=(value) foo = value end alias :foo :login alias :foo= :login end so.. how should the define methods look like? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If you''re trying to bend the login engine plugin into a schema that really doesn''t match its assumptions, you''re probably better copying the code into your application properly, and then just adjusting it and it''s migrations directly - this kind of hacking (while certainly possible) is just going to make things more complicated in the long run... FYI, ActiveRecord has an ''alias_attribute'' method which does exactly what you''re looking for, i.e. class MyModel < ActiveRecord::Base alias_attribute :your_name, :the_actual_column_name end This might only be in Edge Rails though; Caveat Coder. - James On 12/6/06, Guest <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Josh Susser wrote: > > Wes Gamble wrote: > >> Anybody know of a way to alias a column in ActiveRecord such that you > >> could reference it via a different symbol in your view for view purposes? > >> > >> Like my column is named X but I want to display it as Y and maybe I have > >> an accessor named Y set up on the model so that when I "call" Y from the > >> view (and controller?) I''m actually manipulating the column X? > > > > Use Ruby''s ''alias'' feature. > > > > alias :y :x > > alias :y= :x> > > > Make sure you alias after the :x methods are defined, because a later > > ''def x'' will create a new :x method and :y will point to the old :x. > > > > --josh > > this is probably well and all for an experianced programmer to > understand. But I''m a total noob, so how would you for instance alias a > column name namned "login" to the alias "foo" in a table namned "users" > > Tried something like this.. and it does not work: > > class User < ActiveRecord::Base > include LoginEngine::AuthenticatedUser > > belongs_to :account > > def login > foo > end > > def login=(value) > foo = value > end > > alias :foo :login > alias :foo= :login> > end > > so.. how should the define methods look like? > > -- > Posted via http://www.ruby-forum.com/. > > > >-- * J * ~ --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wow, that''s an old original post. When I need to do display that modifies the original attribute in some way on the way in or out, I do something like this: def zip_code zip = read_attribute(:Zip) zip.nil? ? '''' : zip.strip end def zip_code=(new_zip) write_attribute(:Zip, new_zip.strip) end where "Zip" is the column in the table. I don''t use method aliasing to accomplish this although I suppose that I could. I tend to like explicitly using the read/write attribute functions to remind me of all of the layers of attribute management which are involved. Wes -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---