I'm new to Ruby on Rails, and I'm developing a backend API.
Currently, I got 2 Active Record Models called Book and User.
Active Record Model
class Book < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :books
end
DB Schema Model
create_table :books do |t|
t.string "title"
end
create_table :users do |t|
t.string "name"
end
#User favourite books
create_join_table :users, :books do |t|
t.index [:user_id, :book_id]
t.index [:book_id, :user_id]
end
#User read books
create_join_table :users, :books do |t|
t.index [:user_id, :book_id]
t.index [:book_id, :user_id]
t.integer "read_pages"
t.string "status"
t.integer "rating"
t.datetime "start_date"
t.datetime "finish_date"
end
QUESTION
I'd like to create 2 join tables, one for those books added to the user favourites list, another one for those books that a user has read.
Both tables share user_id & book_id, howerver, the second one has more data since it is a record.
- Active Record naming convention creates a table named as users_books automatically. So when I migrate this, it reports to me the following error:
Index name 'index_books_users_on_user_id_and_book_id' on table 'books_users' already exists
- How do I rename the second join table name?