我正在為不斷增長的 incel 在線社區制作一個基本的應用程式,將他們鏈接到他們認為有用、有幫助和指導的各種文學作品。我很難建立適當的關聯。
我有幾個模型:
- 用戶:user.rb,查看、評論和點贊圖書的用戶模型。
- 喜歡:like.rb,為書籍分配喜歡的模型。
- 評論:comment.rb,評論模型以評論書籍模型(通過用戶)。
- book:book.rb,書籍模型,將路由/查看 pdf 到主機服務器。
- bookshares:book_share.rb,將是一個連接表,將用戶鏈接到喜歡的書籍評論等,反之亦然。
- godmodel:尚未實施的假設模型,無法以無所不包的方式將所有內容連接在一起。
因此,我希望用戶能夠使用用戶名創建,并能夠在“網站”上查看、喜歡和評論最終將遷移到 Android 應用程式的書籍。這是我糟糕的代碼:
class BookShare < ApplicationRecord
validates :book_id, presence: true, uniqueness: true
validates :viewer_id, presence: true, uniqueness: true
belongs_to :user
belongs_to :book
belongs_to :comment
end
class Book < ApplicationRecord
validates :title, presence: true
validates :author, presence: true
validates :user_id, presence: true
validates :isbn, presence: true
validates :title, presence: true
has_many :book_shares
has_many :users, through: :book_shares
has_many :likes, through: :book_shares
has_many :comments, through: :book_shares
end
class Comment < ApplicationRecord
validates :user_id, presence: true, uniqueness: true
validates :book_id, presence: true, uniqueness: true
validates :title, presence: true
has_many :book_shares
has_many :books, through: :book_shares
end
class GodModel < ApplicationRecord
has_many :books
has_many :users
has_many :comments
#has_many :likes
has_many :topics
has_many :book_shares
end
class Like < ApplicationRecord
# not fully implemented yet.
validates :user_id, presence: true
belongs_to :user
end
class User < ApplicationRecord
validates :username, presence: true, uniqueness: true
has_many :book_shares
has_many :comments, through: :book_shares
has_many :books, through: :book_shares
end
class Topic < ApplicationRecord
# not implemented yet
end
這是我的架構:
ActiveRecord::Schema[7.0].define(version: 2022_04_12_145402) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "book_shares", force: :cascade do |t|
t.integer "book_id", null: false
t.integer "viewer_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["book_id", "viewer_id"], name: "index_book_shares_on_book_id_and_viewer_id", unique: true
t.index ["book_id"], name: "index_book_shares_on_book_id"
t.index ["viewer_id"], name: "index_book_shares_on_viewer_id"
end
create_table "books", force: :cascade do |t|
t.string "title", null: false
t.string "author", null: false
t.integer "user_id", null: false
t.text "body_info"
t.integer "isbn", null: false
t.binary "photo"
t.binary "r_data"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_books_on_user_id"
end
create_table "comments", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "book_id", null: false
t.text "body_txt"
t.string "title"
t.binary "photo"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["book_id"], name: "index_comments_on_book_id"
t.index ["user_id"], name: "index_comments_on_user_id"
end
create_table "god_models", force: :cascade do |t|
t.string "title"
t.integer "user_id"
t.integer "comment_id"
t.integer "book_share_id"
t.integer "book_id"
t.integer "like"
t.integer "topic"
t.binary "data_x"
t.date "today"
t.binary "title2"
t.boolean "nullfy"
t.float "nums"
t.text "body"
t.datetime "create_at_"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["body"], name: "index_god_models_on_body"
t.index ["book_id"], name: "index_god_models_on_book_id"
t.index ["book_share_id"], name: "index_god_models_on_book_share_id"
t.index ["comment_id"], name: "index_god_models_on_comment_id"
t.index ["data_x"], name: "index_god_models_on_data_x"
t.index ["like"], name: "index_god_models_on_like"
t.index ["nullfy"], name: "index_god_models_on_nullfy"
t.index ["nums"], name: "index_god_models_on_nums"
t.index ["title2"], name: "index_god_models_on_title2"
t.index ["today"], name: "index_god_models_on_today"
t.index ["topic"], name: "index_god_models_on_topic"
t.index ["user_id"], name: "index_god_models_on_user_id"
end
create_table "likes", force: :cascade do |t|
t.integer "user_id"
t.string "likeable_type"
t.bigint "likeable_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["likeable_type", "likeable_id"], name: "index_likes_on_likeable"
t.index ["user_id", "likeable_type", "likeable_id"], name: "index_likes_on_user_id_and_likeable_type_and_likeable_id", unique: true
end
create_table "users", force: :cascade do |t|
t.string "username", null: false
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["username"], name: "index_users_on_username", unique: true
end
end
示例控制器:
class UsersController < ApplicationController
def index
render plain: 'index'
end
def show
render plain: 'show'
end
def new
render plain: 'new'
end
def destroy
render plain: 'destroy'
end
def update
redner update: 'update'
end
private
def usershare_param
params.require(:user).permit(:username)
end
end
這是我迄今為止制作的。我能夠創建模型物件,保存它們我認為并填充它們的欄位,但我不認為我的模型正在使用給定的關聯。
我嘗試使用 erd 但它不起作用。鑒于我的應用程式的使用,模型/關聯是否正確?我希望有一個用戶可以查看/評論/喜歡感興趣的書籍。一本書可以有很多點贊和評論,圖書可以被很多用戶查看,用戶可以點贊和評論很多書,主題將在以后實作對圖書進行分類。通過用戶喜歡/評論/查看的整個機制將通過稱為 bookshares 的連接表實作。在移動到迷你保護的視圖/路線部分之前,我想正確地撰寫我的關聯。
uj5u.com熱心網友回復:
有 4 個表、books、和。您可以實作給定的設計。userscommentslikes
一本書可以有很多喜歡(用戶),一個用戶可以喜歡很多本書,形成用戶和書籍之間的多對多關系。制作likes一個連接表。
同樣,一本書有很多評論,一個用戶可以寫很多評論。在comments用戶和書籍之間創建一個連接表,其中包含特定于評論的額外欄位。
# app/models/book.rb
has_many :likes, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :lovers, through: :likes, source: :user
has_many :commentors, through: :comments, source: :user
# app/models/user.rb
has_many :likes, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :fav_books, through: :likes, source: :book
# app/models/like.rb
belongs_to :book
belongs_to :user
# app/models/comment.rb
belongs_to :book
belongs_to :user
您可以查閱本指南以更深入地探索主題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460569.html
上一篇:將所有零值推到最后-RubyonRails(Postgresql)
下一篇:Mac安裝Homebrew
