I have two tables: Table [packages] id name user_name ------------------- 1 PK1 Ray ------------------- Table [users] id name full_name ------------------- 2 Ray Ray Sun ------------------- My purpose is to find the full name when I find packages. So I modify Model Package to: class Package < ActiveRecord::Base belongs_to :user, :class_name => "User", :foreign_key => "user_name" end Then I run Package.find(1, :include => :user) in Rails console. From the log, I can see the sql is: Package Load Including Associations (0.000309) SELECT `packages`.`id` AS t0_r0, `packages`.`name` AS t0_r1, `packages`.`user_name` AS t0_r2, `users`.`id` AS t1_r0, `users`.`name` AS t1_r1, `users`.`full_name` AS t1_r2 FROM `packages` LEFT OUTER JOIN `users` ON `users`.id = `packages`.user_name WHERE (`packages`.`id` = 1) But I cannot find the parameters for belongs_to to set associated_foreign_key like has_and_belongs_to_many. I don''t know how to deal with it. Please help. Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
On 4 Jan 2009, at 08:20, Ray Sun wrote:> > Then I run Package.find(1, :include => :user) in Rails console. From > the > log, I can see the sql is: > Package Load Including Associations (0.000309) SELECT > `packages`.`id` AS t0_r0, `packages`.`name` AS t0_r1, > `packages`.`user_name` AS t0_r2, `users`.`id` AS t1_r0, `users`.`name` > AS t1_r1, `users`.`full_name` AS t1_r2 FROM `packages` LEFT OUTER JOIN > `users` ON `users`.id = `packages`.user_name WHERE (`packages`.`id` > = 1) > > But I cannot find the parameters for belongs_to to set > associated_foreign_key like has_and_belongs_to_many. I don''t know > how to > deal with it. Please help. Thanks.There is no such option for belongs_to. In the rails world your packages table would have a user_id column. Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 4 Jan 2009, at 08:20, Ray Sun wrote: >> >> But I cannot find the parameters for belongs_to to set >> associated_foreign_key like has_and_belongs_to_many. I don''t know >> how to >> deal with it. Please help. Thanks. > > There is no such option for belongs_to. In the rails world your > packages table would have a user_id column. > > FredIs there any way as workaround to satisfy my requirement? Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
On 4 Jan 2009, at 13:39, Ray Sun wrote:> > Frederick Cheung wrote: >> On 4 Jan 2009, at 08:20, Ray Sun wrote: >>> >>> But I cannot find the parameters for belongs_to to set >>> associated_foreign_key like has_and_belongs_to_many. I don''t know >>> how to >>> deal with it. Please help. Thanks. >> >> There is no such option for belongs_to. In the rails world your >> packages table would have a user_id column. >> >> Fred > > Is there any way as workaround to satisfy my requirement? Thanks.Nothing obvious springs to mind. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---