I’m having some trouble modeling my data with many-to-many associations. I recently purchased Programming Ecto and have been going through it, and while it has illuminated some things for me, I still can’t seem to get my application working quite right, and was hoping I could get some help.
What I am trying to do is create an application to help people manage their books. For my data modeling, I have a user, and the user can have many books, as well as many collections of books. Also, books and collections have a many-to-many relationship, as a book could be in many collections, and a collection can have many books. My schemas so far look something like this.
schema "users" do field :name, :string field :username, :string has_one :credential, Credential has_many :books, Book has_many :collections, Collection timestamps() end
schema "books" do field :author, :string field :title, :string field :isbn, :string field :thumbnail, :string belongs_to :user, User many_to_many :collections, Collection, join_through: CollectionsBooks timestamps() end
schema "collections" do field :name, :string belongs_to :user, User many_to_many :books, Book, join_through: CollectionsBooks timestamps() end
schema "collections_books" do belongs_to :books, Book belongs_to :collections, Collection timestamps() end
I’m trying to hook this all up through Phoenix, and I’ve just been having a rough time getting everything to wire up properly. Do you folks have any advice on how to structure some basic CRUD operations with the above model, or how I can improve the model in general?