【发布时间】:2018-11-11 20:39:39
【问题描述】:
我目前正在尝试构建一个与Trello 类似的应用程序。我有users 可以创建boards。每个用户可以拥有不同的role,具体取决于他们所在的板。因为我对 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 Boards 和Board has_many Users 两个through: UserBoard。根据User 和Board,它们可以有不同的role。
user_board.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
board.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 的原因是因为当我尝试使用new 或build 时,连接表中的关联没有建立。我无法执行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.. 在我将记录保存在board#create 操作中之后,我能想到的唯一方法是current_user.user_boards.find_by(board_id:@board).update_attribute(:role, 'admin')。这个查询有点让我想呕吐,我不敢相信没有更好的方法。
我的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 框架来学习/熟悉自己。
【问题讨论】:
标签: ruby-on-rails ruby activerecord relational-database jointable