Vincent Lin wrote in post #1070656:
This is my source codes.
I got two models, Book and Word.
class Book < ActiveRecord::Base
class Word < ActiveRecord::Base
I got a Join Tables for has_and_belongs_to_many Associations
class CreateBooksWords < ActiveRecord::Migration
create_table :books_words, :id => false do |t|
Now I want to create a form, and user can insert words into a book.
How do I do that?
There are some conceptual issues here.
If this is in fact a many-to-many relationship between books and words
so that, a book can have many words, and a word can be in many books,
Don’t think of “i want to add words to a book”, but think of “I have a
word, and i have a book, and i want to build a relationship between the
So the form you’re talking about is not a form on words and books, but a
form about a single model: book_word_relationship (name it whatever you
like). You will have a relationship_controller which will handle #create
and #destroy actions. At this point the form should be trivial. The
#create action will take as parameters the book_id and word_id. Or more
This means you will need a proper model and controller to handle these
relationships. Which also means that a HABTM relationship is not
appropriate and you should use has_many :through=>‘relationship’.
This railscast has some good advice about the front-end of a many-many
don’t worry about the self-referential part.