一本书有_许多章节。一个用户有_many 的书。
我正在使用 devise 和 cancancan,并且我希望允许用户:为他们拥有的任何书创建一个章节。我的问题是,一章不属于一本书,直到它存在于能力.rb中 -
can [:read, :update, :destroy], Chapter do |chapter|
chapter.book.user_id == user.id
end
很好,但是在 :create 上使用 this 会抛出一个 nil 方法,因为在创建对象之前,它没有任何属性。我已经在控制器中的创建操作中进行了检查-
if @chapter.book.user == current_user && @chapter.save
...这很好,但必须有更好的方法,对吧?