感謝您的閱讀!在我的應用中實現用戶角色的方式
我目前的工作在我的新的應用程序,並尋找最好的方式來實現一個功能
通過情景我需要實現
我「作爲一個用戶的位置有作用」已完成:
場景: 當用戶將新位置添加到配置文件 其中一個需要的字段是「角色」。這可能是「客人」,「經理」或「賣家」。在模型方面完成他的最好方法是什麼?
我做到了這一點與has_many_through assosiation
控制器:
def create
@location = Location.new(location_params)
@location.profiles << current_user.profile
#set user role
current_user.profile.profile_location_throughs.where(location_id: location.id).set_role(params[:location][:role])
respond_to do |format|
if @location.save
....
end
end
end
機型:
class Profile < ActiveRecord::Base do
has_many :profile_location_throughs
has_many :locations, through: :profile_location_throughs
end
class Location < ActiveRecord::Base do
has_many :profile_location_throughs
has_many :locations, through: :profile_location_throughs
end
class ProfileLocationThrough < ActiveRecord::Base
# with boolean fields: manager, seller, guest
belongs_to :location
belongs_to :profile
def set_role(role)
case role
when "guest"
self.guest = true
when "seller"
self.seller = true
when "manager"
self.manager = true
end
end
end
=====
問: 你可以建議更多beatiful方式實現他的功能?
可以將用戶在不同的地點不同的角色?如果是的話,通過關聯來管理是正確的。但是,並不是只保留通過表的每個記錄中的角色名稱。製作一個角色表和相應的角色模型,並通過表格添加一個關聯。 –
我看到你有3個不同的領域,每個角色一個。看,這可能很糟糕,因爲無論何時定義新角色,您都必須向模型添加新字段。相反,您應該保留單個字段「角色」並將「客人」,「經理」或「賣家」保存在其中。 –
是的。在一個地點,它可能是「賣家」,在其他「客人」等等 – Art