这些是我的联想:
class User
has_many :products
has_many :prices
has_many :businesses
has_many :stores
end
class Price
belongs_to :store
belongs_to :user
belongs_to :product
end
class Store
belongs_to :user
belongs_to :business
has_many :prices
end
class Business
belongs_to :user
has_many :stores
end
class Product
belongs_to :user
has_many :prices
end
这些是我所做的更改,以便关联正常工作:
所有模型的表中都有 user_id。
我像上面一样把关联放在里面。
我让 CanCan 能力反映了这些变化
def initialize(user) if user.role == "admin" can :manage, :all elsif user.role == "default" can :manage, [Price, Store, Business, Product], :user_id => user.id can :read, [Product, Store, Business, Price] end end
如果有助于了解,我正在使用设计。
当我想创建一个企业时,它允许我,但他们没有分配用户。我不知道是什么问题。我认为它会自动分配自己,就像以前用户有很多价格时一样。你认为问题是什么?
编辑
class BusinessesController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
def show
@business = Business.find(params[:id])
end
def new
@business = Business.new
end
def create
@business = Business.new(params[:business])
if @business.save
redirect_to new_business_path, :notice => "Successfully added."
else
render :new, :notice => "It seems there was an error. Please try again."
end
end
end