Hi I have two models . Dbase -------- name vendor type port defuser - default user name Users ------- username password email I need to build an association between these two in that the dbase model''s defuser needs to be present in the users table. I am from a relational database background so am trying hard to understand rails associations. What should I do to associate the dbase''s defuser - > users.username? I read the documents and everything was based on the column ''id''. Any help is appreciated. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Are you trying to link Dbase to a user as defuser (default_user)? If so, this would work class Dbase < ActiveRecord::Base belongs_to(:defuser, :class_name => ''User'', :foreign_key => ''defuser'') end It would be better rails practice to call the column "default_user_id", and then make the association like this: class Dbase < ActiveRecord::Base belongs_to(:default_user, :class_name => ''User'') end Btw, the column name "type" is a reserved rails. It is used for single table inheritance. Unless you are using single table inheritance here, I suggest you rename it to dbase_type, otherwise you may encounter strange error messages. On Apr 28, 7:34 am, Arun Srini <arunro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > I have two models . > Dbase > -------- > name > vendor > type > port > defuser - default user name > > Users > ------- > username > password > email > > I need to build an association between these two in that the dbase > model''s defuser needs to be present in the users table. I am from a > relational database background so am trying hard to understand rails > associations. > What should I do to associate the dbase''s defuser - > users.username? > I read the documents and everything was based on the column ''id''. > > Any help is appreciated. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for the code and also for the note. I''ve changed the var to db_type. Also, what is the difference between having the :foreign_key => ''defuser'' and not, in your case of the first code and the 2nd? Also, is there an IRC channel for rails? On Apr 28, 1:52 pm, Sharagoz <shara...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Are you trying to link Dbase to a user as defuser (default_user)? > If so, this would work > > class Dbase < ActiveRecord::Base > belongs_to(:defuser, :class_name => ''User'', :foreign_key => > ''defuser'') > end > > It would be better rails practice to call the column > "default_user_id", and then make the association like this: > > class Dbase < ActiveRecord::Base > belongs_to(:default_user, :class_name => ''User'') > end > > Btw, the column name "type" is a reserved rails. It is used for single > table inheritance. > Unless you are using single table inheritance here, I suggest you > rename it to dbase_type, otherwise you may encounter strange error > messages. > > On Apr 28, 7:34 am, Arun Srini <arunro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi > > I have two models . > > Dbase > > -------- > > name > > vendor > > type > > port > > defuser - default user name > > > Users > > ------- > > username > > password > > email > > > I need to build an association between these two in that the dbase > > model''s defuser needs to be present in the users table. I am from a > > relational database background so am trying hard to understand rails > > associations. > > What should I do to associate the dbase''s defuser - > users.username? > > I read the documents and everything was based on the column ''id''. > > > Any help is appreciated. > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.