My question is similar to Build vs new in Rails 3 .
In Rails 3, I could create an object in a view to verify authorization through cancan.
<% if can? :create, @question.answers.new %>
In Rails 3, the difference between .new and .build was that .build added the newly created object to the parent collection, which led to an extra record in the view, which was clearly not desirable.
In Rails 4, however, both add an object to the collection, creating an empty entry in the view.
Does anyone have any tips on how to solve this? Verifying that the .persisted? entry .persisted? in the view would be an option, but somehow I feel like I don't need to do this.
Edit: To clarify, the CanCan model looks like this:
can :manage, Answer do |answer| user.belongables.include?(answer.question.try(:belongable)) end
Because of this, I cannot just check the class. The actual instance is really necessary for comparison based on the relationship.
activerecord ruby-on-rails-4
pduersteler
source share