Tyler Prete
2007-Jan-20 20:05 UTC
Aliasing Active Record Attributes (also delegation issues)
This whole situation is a mess, but I''ll see if I can explain it well.
I have two models, User and Company. I''d like user to be able to get
company.name. I''m aware of the problems with using @user.company.name,
because of company is nil, then name throws an error.
So originally, I wrote this method instead
class User
def company_name
company ? company.name : nil
end
end
This works, however, rather than use that, I would like to use delegation.
The problem I ran into with that,
however, is that user also has a name. So I can''t simply write
class User
delegate :name, :to => :company
end
because that will overwrite name. It would be nice if there was a delegate
:company_name, :as => :name, :to => :company, but as far as I know this
doesn''t exist. So... I thought I would use "delegate
:company_name: to =>
:company", and alias :company_name to :name within company, like so:
class Company
alias :company_name :name
end
however, that will not work with Active Record models, because name is not
defined until the class is instantiated. I could just write a wrapper
method, aka def class_name; name; end; but that adds an extra method call
that I''d rather not have. So finally, I came up with this solution:
class Company
def company_name
name
self.class.class_eval do
alias_method :company_name, :name
end
name
end
end
This works, because the first time company_name is called, it calls name to
instantiate it (needed the first call for some reason), then it opens the
class and aliases company_name to name. Lastly, it returns name, so that it
works as intended on the first call as well.
This code does work, I have tested it, but it seems like an awful lot of
work for what seems like a simple concept. Maybe I''m just making
things too
complicated, and should have simply used my company_name method within User,
I don''t know.
If anyone can suggest a better way to solve this problem, it would be much
appreciated.
Thanks,
Tyler Prete
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-21 04:02 UTC
Re: Aliasing Active Record Attributes (also delegation issues)
I''m not entirely sure, but i think @object.content.name only trows an error if @object.content is nil AND you''re in development mode (whiny nils is enabled). Not sure though. On Jan 20, 9:05 pm, "Tyler Prete" <psyo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This whole situation is a mess, but I''ll see if I can explain it well. > > I have two models, User and Company. I''d like user to be able to get > company.name. I''m aware of the problems with using @user.company.name, > because of company is nil, then name throws an error. > > So originally, I wrote this method instead > > class User > def company_name > company ? company.name : nil > end > end > > This works, however, rather than use that, I would like to use delegation. > The problem I ran into with that, > however, is that user also has a name. So I can''t simply write > > class User > delegate :name, :to => :company > end > > because that will overwrite name. It would be nice if there was a delegate > :company_name, :as => :name, :to => :company, but as far as I know this > doesn''t exist. So... I thought I would use "delegate :company_name: to => > :company", and alias :company_name to :name within company, like so: > > class Company > alias :company_name :name > end > > however, that will not work with Active Record models, because name is not > defined until the class is instantiated. I could just write a wrapper > method, aka def class_name; name; end; but that adds an extra method call > that I''d rather not have. So finally, I came up with this solution: > > class Company > def company_name > name > self.class.class_eval do > alias_method :company_name, :name > end > name > end > end > > This works, because the first time company_name is called, it calls name to > instantiate it (needed the first call for some reason), then it opens the > class and aliases company_name to name. Lastly, it returns name, so that it > works as intended on the first call as well. > This code does work, I have tested it, but it seems like an awful lot of > work for what seems like a simple concept. Maybe I''m just making things too > complicated, and should have simply used my company_name method within User, > I don''t know. > > If anyone can suggest a better way to solve this problem, it would be much > appreciated. > > Thanks, > Tyler Prete--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris Wanstrath
2007-Jan-21 09:48 UTC
Re: Aliasing Active Record Attributes (also delegation issues)
On 1/20/07, Tyler Prete <psyonic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Company > alias :company_name :name > endclass Company alias_attribute :company_name, :name end -- Chris Wanstrath http://errtheblog.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 -~----------~----~----~----~------~----~------~--~---
Tyler Prete
2007-Jan-21 14:45 UTC
Re: Aliasing Active Record Attributes (also delegation issues)
Chris, Thanks! Much much better. I had the feeling that there had to be a better way, and there it is. --Tyler P.S. I really like your blog. Lots of good information on there. On 1/21/07, Chris Wanstrath <chris-G5sj8e7vJc8@public.gmane.org> wrote:> > > On 1/20/07, Tyler Prete <psyonic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > class Company > > alias :company_name :name > > end > > class Company > alias_attribute :company_name, :name > end > > -- > Chris Wanstrath > http://errtheblog.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 -~----------~----~----~----~------~----~------~--~---