thanks all... problem solved.

2009/7/3 Colin Law <[email protected]>

>
> 2009/7/3 gaoxh gaoxh04 <[email protected]>:
> > If I remove id column, what is the primary key?
>
> Have a look at the rails guides, particularly the one on ActiveRecord
> associations at http://guides.rubyonrails.org/index.html
>
> Colin
>
> >
> > 2009/7/3 Marnen Laibow-Koser <[email protected]>
> >>
> >> Xiahong Gao wrote:
> >> > Hi everyone,
> >> >
> >> > I ran into a serious problem when I was trying to do something similar
> >> > to twitter follow mechanism. The thing is like this:
> >> >
> >> > First, I have a self-referential table, named "users"
> >> >   create_table :users do |t|
> >> >       t.column :name, :string
> >> >       t.column :user_name, :string
> >> >       t.column :password, :string
> >> >       t.column :email, :string
> >> >   end
> >> >
> >> > I also have a HABTM join table, named "follow_relations"
> >> >   create_table :follow_realtions do |t|
> >> >       t.column :a_id, :integer  # a_id follows b_id
> >> >       t.column :b_id, :integer
> >> >   end
> >> >
> >> > My HABTM join is defined as follows
> >> >    has_and_belongs_to_many :followers,
> >> >                                                  :class_name =>
> >> > "User",
> >> >                                                  :join_table =>
> >> > "follow_relations",
> >> >                                                  :foreign_key =>
> >> > "b_id",
> >> >
> >> >  :association_foreign_key
> >> > => "a_id"
> >> >
> >> >   has_and_belongs_to_many :followings,
> >> >                                                 :class_name => "User",
> >> >                                                 :join_table =>
> >> > "follow_relations",
> >> >                                                 :foreign_key =>
> >> > "a_id",
> >> >
> :association_foreign_key
> >> > => "b_id"
> >> [...]
> >> > The first << operation executes the following SQL :
> >> > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 1, 3)
> >> > The second << operation executes the following SQL:
> >> > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 2, 3)
> >> > Thus, key id is duplicated. It looks like that every time, the value
> >> > of column id is copied from column a_id.
> >> > What's going on here? How to fix this problem?
> >>
> >> If you're using HABTM, the join table (follow_relations) should not have
> >> an id column.  Remove it and see if that does the trick.
> >>
> >> >
> >> > Thanks in advance,
> >> > xiahong
> >>
> >> Best,
> >> --
> >> Marnen Laibow-Koser
> >> http://www.marnen.org
> >> [email protected]
> >> --
> >> 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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to