Rails 4 has_many :through relationship: 将默认值分配给从父模型创建操作时加入模型属性
Rails 4 has_many :through relationship: assign default value to join model attribute on create action from parent model
我有三个型号:
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
end
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
备注:我在User
模型上使用Devise进行身份验证。
连接 Administration
模型具有以下属性:
id
user_id
calendar_id
role
这是我当前的路线:
devise_for :users, :path => 'account'
resources :users do
resources :calendars
end
这是我的 calendars_controller
:
class CalendarsController < ApplicationController
before_action :set_calendar, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /calendars
# GET /calendars.json
def index
@user = current_user
@calendars = Calendar.all
end
# GET /calendars/1
# GET /calendars/1.json
def show
@user = current_user
@calendar = Calendar.find(params[:id])
end
# GET /calendars/new
def new
@user = current_user
@calendar = @user.calendars.new
end
# GET /calendars/1/edit
def edit
@user = current_user
end
# POST /calendars
# POST /calendars.json
def create
@user = current_user
@calendar = @user.calendars.new(calendar_params)
respond_to do |format|
if @calendar.save
format.html { redirect_to user_calendar_path(@user,@calendar), notice: 'Calendar was successfully created.' }
format.json { render :show, status: :created, location: @calendar }
else
format.html { render :new }
format.json { render json: @calendar.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /calendars/1
# PATCH/PUT /calendars/1.json
def update
@user = current_user
@calendar = Calendar.find(params[:id])
respond_to do |format|
if @calendar.update(calendar_params)
format.html { redirect_to user_calendar_path(@user,@calendar), notice: 'Calendar was successfully updated.' }
format.json { render :show, status: :ok, location: @calendar }
else
format.html { render :edit }
format.json { render json: @calendar.errors, status: :unprocessable_entity }
end
end
end
# DELETE /calendars/1
# DELETE /calendars/1.json
def destroy
@user = current_user
@calendar.destroy
respond_to do |format|
format.html { redirect_to user_calendars_url, notice: 'Calendar was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_calendar
@calendar = Calendar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def calendar_params
params.require(:calendar).permit(:name)
end
end
我的基本 CRUD 操作有效:user
登录后,他可以 create
、show
、edit
和 destroy
calendar
属于他的。
我现在想弄清楚的是,当 user
创建新 calendar
.
我正在考虑将以下代码行添加到 Calendars#Create
:
current_user.add_calendar_and_role(@calendar.id, 'Owner')
但我不确定这是一个好的做法。
相反,我不应该在我的 administration
模型中定义一个 make_owner
方法并在 Calendars#Create
操作中将其与回调一起使用吗?
我是否也应该在我的 calendar
模型中实施 accepts_nested_attributes_for
?
或者是否有我忘记的更好的解决方案?
总的来说 - 尽可能将代码移至模型。让您的控制器保持简单。
我认为您不需要接受嵌套属性。当我的模型是真正的子对象时,我倾向于使用它,而不仅仅是连接。
你的数据模型看起来不错 - 我想无论你选择什么,你都可以在以后改进它或处理更多的特殊情况。也就是说,我做这种事
class Administration < ActiveRecord::Base
before_validation :set_default_role
def set_default_role
existing_owner = where(:calendar_id => calendar_id, :role => :owner).exists?
if existing_owner?
self.role ||= :standard
else
self.role ||= :owner
end
end
end
我有三个型号:
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
end
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
备注:我在User
模型上使用Devise进行身份验证。
连接 Administration
模型具有以下属性:
id
user_id
calendar_id
role
这是我当前的路线:
devise_for :users, :path => 'account'
resources :users do
resources :calendars
end
这是我的 calendars_controller
:
class CalendarsController < ApplicationController
before_action :set_calendar, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /calendars
# GET /calendars.json
def index
@user = current_user
@calendars = Calendar.all
end
# GET /calendars/1
# GET /calendars/1.json
def show
@user = current_user
@calendar = Calendar.find(params[:id])
end
# GET /calendars/new
def new
@user = current_user
@calendar = @user.calendars.new
end
# GET /calendars/1/edit
def edit
@user = current_user
end
# POST /calendars
# POST /calendars.json
def create
@user = current_user
@calendar = @user.calendars.new(calendar_params)
respond_to do |format|
if @calendar.save
format.html { redirect_to user_calendar_path(@user,@calendar), notice: 'Calendar was successfully created.' }
format.json { render :show, status: :created, location: @calendar }
else
format.html { render :new }
format.json { render json: @calendar.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /calendars/1
# PATCH/PUT /calendars/1.json
def update
@user = current_user
@calendar = Calendar.find(params[:id])
respond_to do |format|
if @calendar.update(calendar_params)
format.html { redirect_to user_calendar_path(@user,@calendar), notice: 'Calendar was successfully updated.' }
format.json { render :show, status: :ok, location: @calendar }
else
format.html { render :edit }
format.json { render json: @calendar.errors, status: :unprocessable_entity }
end
end
end
# DELETE /calendars/1
# DELETE /calendars/1.json
def destroy
@user = current_user
@calendar.destroy
respond_to do |format|
format.html { redirect_to user_calendars_url, notice: 'Calendar was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_calendar
@calendar = Calendar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def calendar_params
params.require(:calendar).permit(:name)
end
end
我的基本 CRUD 操作有效:user
登录后,他可以 create
、show
、edit
和 destroy
calendar
属于他的。
我现在想弄清楚的是,当 user
创建新 calendar
.
我正在考虑将以下代码行添加到 Calendars#Create
:
current_user.add_calendar_and_role(@calendar.id, 'Owner')
但我不确定这是一个好的做法。
相反,我不应该在我的 administration
模型中定义一个 make_owner
方法并在 Calendars#Create
操作中将其与回调一起使用吗?
我是否也应该在我的 calendar
模型中实施 accepts_nested_attributes_for
?
或者是否有我忘记的更好的解决方案?
总的来说 - 尽可能将代码移至模型。让您的控制器保持简单。
我认为您不需要接受嵌套属性。当我的模型是真正的子对象时,我倾向于使用它,而不仅仅是连接。
你的数据模型看起来不错 - 我想无论你选择什么,你都可以在以后改进它或处理更多的特殊情况。也就是说,我做这种事
class Administration < ActiveRecord::Base
before_validation :set_default_role
def set_default_role
existing_owner = where(:calendar_id => calendar_id, :role => :owner).exists?
if existing_owner?
self.role ||= :standard
else
self.role ||= :owner
end
end
end