Hi, I have a Project model with the following declaration: belongs_to :liaison, :class_name => "Party", :foreign_key => "liaison" In other words, there is an integer foreign key in the Projects table that maps to a row in a Parties table. The belongs_to ensures that if I call the @project.liaison method, I get a Party object in return. So far so good. Now, when displaying a project, I obviously don''t want to display the liaison id; I want a person''s name. My first solution: alias_method :orig_liaison, :liaison def liaison orig_liaison.first_name + '' '' + orig_liaison.last_name if not orig_liaison.nil? end I know there must be a better way.. The problem with this is that it works in ''show.rhtml'', which calls content_columns, and then does ''@project.send(column.name)''. (Which, if you''re following along, calls the aliased method shown above) In ''edit.rhtml'' however, we have the call to text_field, which takes a method name as argument. This does not call the aliased method, so we simpley get the liaison id, instead of a person''s name, in the text field. What is the best way to get this to work the same across the board? Are my initial associations even set up right? Thanks for any light you can shed. -- James Hughes Web application developer Centre for Health Services and Policy Research Vancouver, BC
On 9/21/05, James Hughes <hughes.james-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have a Project model with the following declaration: > > belongs_to :liaison, :class_name => "Party", :foreign_key => "liaison"<snip extraneous explanation of how belongs_to works..>> Now, when displaying a project, I obviously don''t want to display the > liaison id; I want a person''s name. My first solution: > > alias_method :orig_liaison, :liaison > def liaison > orig_liaison.first_name + > '' '' + orig_liaison.last_name if not orig_liaison.nil? > end > > > The problem with this is that it works in ''show.rhtml'', which calls > content_columns, and then does ''@project.send(column.name)''. (Which, > if you''re following along, calls the aliased method shown above) > > In ''edit.rhtml'' however, we have the call to text_field, which takes a > method name as argument. This does not call the aliased method, so we > simpley get the liaison id, instead of a person''s name, in the text > field. >This happens because, deep in the bowels of form_helper.rb, a method called liaison_before_type_cast is called on my model, instead of just ''Project::liaison''. This returns just the id, instead of the whole Party object. So, I''ve overridden liaison_before_type_cast, and just have it call my already overridden liaison method (see above). Now I see people''s names in my edit form instead of just id''s. This all seems a little messy to me, however; so I''ll let my original questions stand:> What is the best way to get this to work the same across the board? > Are my initial associations even set up right? >Thanks again! -- James Hughes Web application developer Centre for Health Services and Policy Research Vancouver, BC
Yannick Koehler
2005-Sep-22 12:24 UTC
Re: Referencing objects included with ''belongs_to''
Hi James, I am in no way an expert yet with rails, but to me the code you should be doing is something like this: class Liason ... def name first_name + '' '' + last_name end end Then use project.liason.name <http://project.liason.name> to get the project liason''s name. For the edit, if you really want people to edit the liason, you should use a select box where you display the possible liason and have an hyperlink to add additionnal one. You could do this like that: controller: edit @liasons = Liason.find(:all, :order => ''id'').map { |l| l.id <http://l.id>, l.name <http://l.name> }; view: edit <%= select :project, :liason, @liasons %> <%= link_to ''Create New'', :controller => ''liason'', :action => ''new'' %> Hopefully, I got it all right. Let me know if it helped you out. -- Yannick Koehler _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
James Hughes
2005-Sep-22 20:04 UTC
Re: Re: Referencing objects included with ''belongs_to''
On 9/22/05, Yannick Koehler <ykoehler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi James, > > I am in no way an expert yet with rails, but to me the code you should be > doing is something like this: > > class Liason ... > def name > first_name + '' '' + last_name > end > end > > Then use project.liason.name to get the project liason''s name.Thanks for the response. That was my first inclination; however, in searching the list archives I came across a thread[1] that suggested using the :class and :foreign_key params to belongs_to, instead of subclassing my Party class (note that ''liaison'' is not the only Party-type I''m dealing with here: I also have applicants, contacts, and data_sources. I just left those out to keep it simpler) The method overriding that I''ve outlined previously is obviously not going to work: my Party objects are inaccessible should I ever need them again. I''ve built a couple of small rails apps, but this is the first one I''ve done with a schema of any complexity. Maybe it''s time to get cozy with the AR chapters in the PragProg book.> > For the edit, if you really want people to edit the liason, you should use > a select box where you display the possible liason and have an hyperlink to > add additionnal one. You could do this like that: ><code snipped> Yes, something like that is definitely the way to go. Actually I''m porting a php app that (because of the potential for really long select lists), actually popped up a window from which a party could be selected, and then filled in the input on the parent window. Any ajax gurus wanting to weigh in on the best way to do that, go right ahead..> Hopefully, I got it all right. Let me know if it helped you out.Yup, thanks! [1] http://tinyurl.com/9w5hn -- James Hughes Web application developer Centre for Health Services and Policy Research Vancouver, BC