I''m not sure how to have two foreign keys of the same type.
e.g.
class CreateFriends < ActiveRecord::Migration
def self.up
create_table :friends do |t|
t.column :user_id, :integer
t.column :user_id, :integer
t.column :ready, :boolean
end
end
def self.down
drop_table :friends
end
end
Now I can''t have the same name twice, but if i change one of them to
user2_id won''t this break the naming convention.
Any ideas?
Is this a bad idea anyway?
--
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 5/23/07, Bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''m not sure how to have two foreign keys of the same type. > > e.g. > > class CreateFriends < ActiveRecord::Migration > def self.up > create_table :friends do |t| > t.column :user_id, :integer > t.column :user_id, :integer > t.column :ready, :boolean > end > end > > def self.down > drop_table :friends > end > end > > Now I can''t have the same name twice, but if i change one of them to > user2_id won''t this break the naming convention. > > Any ideas?The api docs for ActiveRecord::Associations::ClassMethods describe how you specify associations between your models, including overriding the default foreign key and class name when you don''t follow rails'' naming conventions.> Is this a bad idea anyway? >There are lots of sound reasons for referring to the same table multiple times from a single row. It _could_ also mean that your FK is in the wrong table, if you''re just trying to model a one-to-many relationship. Isak> -- > 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 -~----------~----~----~----~------~----~------~--~---
Just guessing from your model names but it appears that you are
building a "reflexive many-to-many relationship." Reflexive meaning
that you are creating a join table between two rows of a single table:
User <------->> Friend <<-------> User
An excerpt from "Rails Recipies" shows this type of Self Referential
Many-to-Many Releationship:
class AddPeopleAndTheirFriendsRelationship < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.column "name", :string
end
create_table :friends_people, :id => false do |t|
t.column "person_id", :integer
t.column "friend_id", :integer
end
end
def self.down
drop_table :people
drop_table :friends_people
end
end
and the Person model looks like:
class Person < ActiveRecord::Base
has_and_belongs_to_many :friends,
:class_name => "Person",
:join_table => "friends_people",
:association_foreign_key => "friend_id",
:foreign_key => "person_id"
end
On May 23, 10:58 am, "Isak Hansen"
<isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 5/23/07, Bob
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
>
>
>
>
>
> > I''m not sure how to have two foreign keys of the same type.
>
> > e.g.
>
> > class CreateFriends < ActiveRecord::Migration
> > def self.up
> > create_table :friends do |t|
> > t.column :user_id, :integer
> > t.column :user_id, :integer
> > t.column :ready, :boolean
> > end
> > end
>
> > def self.down
> > drop_table :friends
> > end
> > end
>
> > Now I can''t have the same name twice, but if i change one of
them to
> > user2_id won''t this break the naming convention.
>
> > Any ideas?
>
> The api docs for ActiveRecord::Associations::ClassMethods describe how
> you specify associations between your models, including overriding the
> default foreign key and class name when you don''t follow
rails'' naming
> conventions.
>
> > Is this a bad idea anyway?
>
> There are lots of sound reasons for referring to the same table
> multiple times from a single row.
>
> It _could_ also mean that your FK is in the wrong table, if you''re
> just trying to model a one-to-many relationship.
>
> Isak
>
> > --
> > Posted viahttp://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
-~----------~----~----~----~------~----~------~--~---
By the way, I would likely model this with the join table as a separate resource using has_many_through. I would call the joining resource Friend. This way you can store information about the association in the Friend model. Such as in your case with :ready => boolean. Or things like friendship_established_on and other such details. On May 23, 11:18 am, Robert Walker <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Just guessing from your model names but it appears that you are > building a "reflexive many-to-many relationship." Reflexive meaning > that you are creating a join table between two rows of a single table: > > User <------->> Friend <<-------> User > > An excerpt from "Rails Recipies" shows this type of Self Referential > Many-to-Many Releationship: > > class AddPeopleAndTheirFriendsRelationship < ActiveRecord::Migration > def self.up > create_table :people do |t| > t.column "name", :string > end > create_table :friends_people, :id => false do |t| > t.column "person_id", :integer > t.column "friend_id", :integer > end > end > > def self.down > drop_table :people > drop_table :friends_people > end > end > > and the Person model looks like: > > class Person < ActiveRecord::Base > has_and_belongs_to_many :friends, > :class_name => "Person", > :join_table => "friends_people", > :association_foreign_key => "friend_id", > :foreign_key => "person_id" > end > > On May 23, 10:58 am, "Isak Hansen" <isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 5/23/07, Bob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > I''m not sure how to have two foreign keys of the same type. > > > > e.g. > > > > class CreateFriends < ActiveRecord::Migration > > > def self.up > > > create_table :friends do |t| > > > t.column :user_id, :integer > > > t.column :user_id, :integer > > > t.column :ready, :boolean > > > end > > > end > > > > def self.down > > > drop_table :friends > > > end > > > end > > > > Now I can''t have the same name twice, but if i change one of them to > > > user2_id won''t this break the naming convention. > > > > Any ideas? > > > The api docs for ActiveRecord::Associations::ClassMethods describe how > > you specify associations between your models, including overriding the > > default foreign key and class name when you don''t follow rails'' naming > > conventions. > > > > Is this a bad idea anyway? > > > There are lots of sound reasons for referring to the same table > > multiple times from a single row. > > > It _could_ also mean that your FK is in the wrong table, if you''re > > just trying to model a one-to-many relationship. > > > Isak > > > > -- > > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---