I have a database table, users, which has a referrer_id column. This column maps back to the users table to denote the user who referred this record. How can I accomplish this type of reference with the t.references call in my create_table without needing to create some sort of crazy (and unnecessary) intermediate table? I''d like the column name to remain referrer_id so the intention of the column is clear. Thanks, Matt --~--~---------~--~----~------------~-------~--~----~ 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 Nov 16, 7:51 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a database table, users, which has a referrer_id column. This > column maps back to the users table to denote the user who referred > this record. How can I accomplish this type of reference with the > t.references call in my create_table without needing to create some > sort of crazy (and unnecessary) intermediate table? I''d like the > column name to remain referrer_id so the intention of the column is > clear. >I''m not sure I understand - you''ve already got the column so why are you worrying about the migration ? t.references doesn''t do any magic - it just adds _id to the end and creates a column of the right type (IIRC in recent versions of rails if it''s also generating the model class it will insert the belongs_to into the model too) Anyway the point is that the migration is dumb, it just adds a column. In terms of making that association work your association will need to specify the :class_name option and so on. Fred> Thanks, > Matt--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So from my understanding of what you''re saying, this would be the line in the migration: t.integer :referrer_id then the users class: class User < ActiveRecord::Base has_one :referrer, :class_name => user end ? I guess it was misleading to say that the column exists. I''ve created the database schema via diagrams and am now trying to map it using migrations. Soft of an exercise for me to learn about all this. Thanks, Matt On Nov 16, 3:09 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 16, 7:51 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a database table, users, which has a referrer_id column. This > > column maps back to the users table to denote the user who referred > > this record. How can I accomplish this type of reference with the > > t.references call in my create_table without needing to create some > > sort of crazy (and unnecessary) intermediate table? I''d like the > > column name to remain referrer_id so the intention of the column is > > clear. > > I''m not sure I understand - you''ve already got the column so why are > you worrying about the migration ? > t.references doesn''t do any magic - it just adds _id to the end and > creates a column of the right type (IIRC in recent versions of rails > if it''s also generating the model class it will insert the belongs_to > into the model too) > > Anyway the point is that the migration is dumb, it just adds a column. > In terms of making that association work your association will need to > specify the :class_name option and so on. > > Fred > > > Thanks, > > Matt--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 16, 8:27 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So from my understanding of what you''re saying, this would be the line > in the migration: t.integer :referrer_id > then the users class: >that would do it.> class User < ActiveRecord::Base > has_one :referrer, :class_name => user > endFirst off the class_name option should be a string ie :class_name => ''User''). Secondly, that should be a belongs_to, ie. belongs_to :referrer, :class_name => ''User'' - if the foreign key is part of the record in question then it''s a belongs to (this distinction is clearer if it''s not an association on the same table). You might also want a second association that tells you all the people that a given user has referred, for that you''d need something along the lines of has_many :somethings, :class_name => ''User'', :foreign_key => ''referrer_id'' Fred> > ? > > I guess it was misleading to say that the column exists. I''ve created > the database schema via diagrams and am now trying to map it using > migrations. Soft of an exercise for me to learn about all this. > > Thanks, > Matt > > On Nov 16, 3:09 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Nov 16, 7:51 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a database table, users, which has a referrer_id column. This > > > column maps back to the users table to denote the user who referred > > > this record. How can I accomplish this type of reference with the > > > t.references call in my create_table without needing to create some > > > sort of crazy (and unnecessary) intermediate table? I''d like the > > > column name to remain referrer_id so the intention of the column is > > > clear. > > > I''m not sure I understand - you''ve already got the column so why are > > you worrying about the migration ? > > t.references doesn''t do any magic - it just adds _id to the end and > > creates a column of the right type (IIRC in recent versions of rails > > if it''s also generating the model class it will insert the belongs_to > > into the model too) > > > Anyway the point is that the migration is dumb, it just adds a column. > > In terms of making that association work your association will need to > > specify the :class_name option and so on. > > > Fred > > > > Thanks, > > > Matt--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great, this is actually exactly what I needed. And just to check my understanding: belongs_to is used if this object (table) holds the referencing key has_one is used if the object (table) you are referencing holds they key back to your id As in: The users table also has a country_id column, so inside the User class I also have belongs_to :country Correct? Thanks, Matt On Nov 16, 3:33 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 16, 8:27 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So from my understanding of what you''re saying, this would be the line > > in the migration: t.integer :referrer_id > > then the users class: > > that would do it. > > > class User < ActiveRecord::Base > > has_one :referrer, :class_name => user > > end > > First off the class_name option should be a string ie :class_name => > ''User''). > Secondly, that should be a belongs_to, ie. > belongs_to :referrer, :class_name => ''User'' - if the foreign key is > part of the record in question then it''s a belongs to (this > distinction is clearer if it''s not an association on the same table). > You might also want a second association that tells you all the people > that a given user has referred, for that you''d need something along > the lines of > > has_many :somethings, :class_name => ''User'', :foreign_key => > ''referrer_id'' > > Fred > > > > > ? > > > I guess it was misleading to say that the column exists. I''ve created > > the database schema via diagrams and am now trying to map it using > > migrations. Soft of an exercise for me to learn about all this. > > > Thanks, > > Matt > > > On Nov 16, 3:09 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On Nov 16, 7:51 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a database table, users, which has a referrer_id column. This > > > > column maps back to the users table to denote the user who referred > > > > this record. How can I accomplish this type of reference with the > > > > t.references call in my create_table without needing to create some > > > > sort of crazy (and unnecessary) intermediate table? I''d like the > > > > column name to remain referrer_id so the intention of the column is > > > > clear. > > > > I''m not sure I understand - you''ve already got the column so why are > > > you worrying about the migration ? > > > t.references doesn''t do any magic - it just adds _id to the end and > > > creates a column of the right type (IIRC in recent versions of rails > > > if it''s also generating the model class it will insert the belongs_to > > > into the model too) > > > > Anyway the point is that the migration is dumb, it just adds a column. > > > In terms of making that association work your association will need to > > > specify the :class_name option and so on. > > > > Fred > > > > > Thanks, > > > > Matt--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 16, 8:46 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > The users table also has a country_id column, so inside the User class > I also have > > belongs_to :countryexactly right. Fred> > Correct? > > Thanks, > Matt > > On Nov 16, 3:33 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Nov 16, 8:27 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So from my understanding of what you''re saying, this would be the line > > > in the migration: t.integer :referrer_id > > > then the users class: > > > that would do it. > > > > class User < ActiveRecord::Base > > > has_one :referrer, :class_name => user > > > end > > > First off the class_name option should be a string ie :class_name => > > ''User''). > > Secondly, that should be a belongs_to, ie. > > belongs_to :referrer, :class_name => ''User'' - if the foreign key is > > part of the record in question then it''s a belongs to (this > > distinction is clearer if it''s not an association on the same table). > > You might also want a second association that tells you all the people > > that a given user has referred, for that you''d need something along > > the lines of > > > has_many :somethings, :class_name => ''User'', :foreign_key => > > ''referrer_id'' > > > Fred > > > > ? > > > > I guess it was misleading to say that the column exists. I''ve created > > > the database schema via diagrams and am now trying to map it using > > > migrations. Soft of an exercise for me to learn about all this. > > > > Thanks, > > > Matt > > > > On Nov 16, 3:09 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > > On Nov 16, 7:51 pm, mlb5000 <baker.mat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a database table, users, which has a referrer_id column. This > > > > > column maps back to the users table to denote the user who referred > > > > > this record. How can I accomplish this type of reference with the > > > > > t.references call in my create_table without needing to create some > > > > > sort of crazy (and unnecessary) intermediate table? I''d like the > > > > > column name to remain referrer_id so the intention of the column is > > > > > clear. > > > > > I''m not sure I understand - you''ve already got the column so why are > > > > you worrying about the migration ? > > > > t.references doesn''t do any magic - it just adds _id to the end and > > > > creates a column of the right type (IIRC in recent versions of rails > > > > if it''s also generating the model class it will insert the belongs_to > > > > into the model too) > > > > > Anyway the point is that the migration is dumb, it just adds a column. > > > > In terms of making that association work your association will need to > > > > specify the :class_name option and so on. > > > > > Fred > > > > > > Thanks, > > > > > Matt--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---