Rails 中与基本图书应用程序的关联?
Associations in Rails for a basic Books app?
我正在为不断增长的 incel 在线社区制作一个基本应用程序 link让他们阅读他们可能会觉得有用、有帮助和指导的各种文学作品。我很难建立正确的关联。
我有几个模型:
- 用户:user.rb,一个用户的模型查看,评论和喜欢书籍。
- 赞:like.rb,点赞模型,为书籍分配点赞数。
- 评论:comment.rb,用于评论书籍模型的评论模型(通过用户)。
- 书籍:book.rb,书籍模型,将 route/viewpdf 发送到主机服务器。
- bookshares:book_share.rb,将加入 table link 用户对书籍的喜欢、评论等,反之亦然。
- godmodel:尚未实现的假设模型 link 以无所不包的方式将所有内容组合在一起。
因此,我希望用户能够使用用户名进行创建,并能够在 'website' 上查看、点赞和评论书籍,这些书籍最终将迁移到 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,但它不起作用。鉴于我的应用程序的使用, models/associations 是否正确制作?我想要一个可以 view/comment/like 感兴趣的书的用户。一本书可以有很多点赞和评论,一本书可以被很多用户浏览,用户可以点赞和评论多本书,后面会实现主题对图书进行分类。 liking/commenting/viewing 通过用户的整个机制将通过称为 bookshares 的连接 table 来实现。在移动到迷你保护的 view/routes 部分之前,我想正确地写下我的关联。
有 4 个 table、books
、users
、comments
和 likes
。您可以实现给定的设计。
一本一本书可以有很多赞(用户),一个用户可以有很多赞图书,用户与图书之间形成many-to-many关系。使 likes
成为联接 table.
同样,一本书有很多评论,一个用户可以写很多评论。在用户和图书之间创建 comments
联接 table,并添加特定于评论的额外字段。
# 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
您可以查阅此 guide 以更深入地探索主题。
我正在为不断增长的 incel 在线社区制作一个基本应用程序 link让他们阅读他们可能会觉得有用、有帮助和指导的各种文学作品。我很难建立正确的关联。
我有几个模型:
- 用户:user.rb,一个用户的模型查看,评论和喜欢书籍。
- 赞:like.rb,点赞模型,为书籍分配点赞数。
- 评论:comment.rb,用于评论书籍模型的评论模型(通过用户)。
- 书籍:book.rb,书籍模型,将 route/viewpdf 发送到主机服务器。
- bookshares:book_share.rb,将加入 table link 用户对书籍的喜欢、评论等,反之亦然。
- godmodel:尚未实现的假设模型 link 以无所不包的方式将所有内容组合在一起。
因此,我希望用户能够使用用户名进行创建,并能够在 'website' 上查看、点赞和评论书籍,这些书籍最终将迁移到 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,但它不起作用。鉴于我的应用程序的使用, models/associations 是否正确制作?我想要一个可以 view/comment/like 感兴趣的书的用户。一本书可以有很多点赞和评论,一本书可以被很多用户浏览,用户可以点赞和评论多本书,后面会实现主题对图书进行分类。 liking/commenting/viewing 通过用户的整个机制将通过称为 bookshares 的连接 table 来实现。在移动到迷你保护的 view/routes 部分之前,我想正确地写下我的关联。
有 4 个 table、books
、users
、comments
和 likes
。您可以实现给定的设计。
一本一本书可以有很多赞(用户),一个用户可以有很多赞图书,用户与图书之间形成many-to-many关系。使 likes
成为联接 table.
同样,一本书有很多评论,一个用户可以写很多评论。在用户和图书之间创建 comments
联接 table,并添加特定于评论的额外字段。
# 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
您可以查阅此 guide 以更深入地探索主题。