gpt4 book ai didi

ruby-on-rails - Ruby on Rails ActiveRecord 数据流

转载 作者:数据小太阳 更新时间:2023-10-29 08:42:50 25 4
gpt4 key购买 nike

我目前正在尝试构建一个类似于 Trello 的应用程序.我有可以创建 boardsusers。每个用户都可以拥有不同的角色,具体取决于他们所在的版 block 。因为我是 Rails 的新手,所以我只想确保我遵循“Rails 方式”和最佳实践。这是我的架构:

create_table "boards", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "user_boards", force: :cascade do |t|
t.integer "user_id"
t.integer "board_id"
t.integer "role"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "lists", "boards"

如您所见,我将 role 属性添加到 UserBoard 连接表。一个 User has_many BoardsBoard has_many Usersthrough: UserBoard。根据用户董事会,他们可以有不同的角色

用户板.rb

class UserBoard < ApplicationRecord
belongs_to :user
belongs_to :board
enum role: { admin: 0, member: 1 }
end

用户.rb

class User < ApplicationRecord
has_many :user_boards
has_many :boards, through: :user_boards
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true
end

棋盘.rb

class Board < ApplicationRecord
has_many :user_boards
has_many :users, through: :user_boards
has_many :lists, dependent: :destroy
include AssociateUsersToBoards

def self.assign_board_to_user(user, board)
AssociateUsersToBoards.build_association(user, board)
end
end

我只是想弄清楚这是否是处理此问题的最佳方法,以及是否有更好的方法来设置它,以便我的查询和更新可以更清晰一些。现在,当 User 创建一个 Board 时,您可以看到我正在使用 ActiveRecord:create 函数。我使用 create 的原因是因为当我尝试使用 newbuild 时,连接表中的关联没有得到。这对我来说似乎是错误的,我将无法执行 current_user.boards.new(board_params)current_user.boards.build(board_params):

def create
@board = current_user.boards.new(board_params)

respond_to do |format|
if current_user.save
format.html { redirect_to @board, notice: 'Board was successfully created.' }
format.json { render :show, status: :created, location: @board }
else
format.html { render :new }
format.json { render json: @board.errors, status: :unprocessable_entity }
end
end
end

但我还想将 role 设置为 admin.. 我能想到的唯一方法是 current_user.user_boards.find_by(board_id: @board).update_attribute(:role, 'admin') 在我将记录保存到我的 board#create 操作之后。这个查询有点让我想吐,我不敢相信没有更好的方法。

我的 board_params 非常简单 & 只允许基本标题:

def board_params
params.require(:board).permit(:title)
end

编辑


我还想添加我用来提交 board 的表单,以确保其中没有任何错误:

<div class="modal-body">
<%= form_with(model: board, local: true) do |form| %>
<% if board.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(board.errors.count, "error") %> prohibited this board from being saved:</h2>

<ul>
<% board.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="form-group">
<%= form.label :title %><br/>
<%= form.text_field :title, autofocus: true, class: 'form-control' %>
</div>

<div class="actions">
<%= form.submit 'Create Board', class: 'btn btn-small btn-success btn-block' %>
</div>
<% end %>
</div>

非常感谢所有建议、提示和建设性批评,因为我尝试越来越多地学习/熟悉 Ruby 语言和 Rails 框架。

最佳答案

你没有显示 board_params,但也许它看起来应该是这样的:

def board_params
params.require(:board).permit(:something, :whatever_else).merge!(role: :admin)
end

然后,做类似的事情:

def create
@board = current_user.boards.new(board_params)

respond_to do |format|
if @board.save
format.html { redirect_to @board, notice: 'Board was successfully created.' }
format.json { render :show, status: :created, location: @board }
else
format.html { render :new }
format.json { render json: @board.errors, status: :unprocessable_entity }
end
end
end

这应该会创建一个正确设置了 user_idrole 的新看板。

顺便说一句,我个人更喜欢:

class UserBoard < ApplicationRecord
belongs_to :user
belongs_to :board
enum role: {
admin: 0,
member: 1
}
end

所以我不必担心 enum 在数组中的位置,如 docs 中讨论的那样:

Note that when an array is used, the implicit mapping from the values to database integers is derived from the order the values appear in the array. In the example, :active is mapped to 0 as it's the first element, and :archived is mapped to 1. In general, the i-th element is mapped to i-1 in the database.

Therefore, once a value is added to the enum array, its position in the array must be maintained, and new values should only be added to the end of the array. To remove unused values, the explicit hash syntax should be used.

关于ruby-on-rails - Ruby on Rails ActiveRecord 数据流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com