My question is similar to Build vs new in Rails 3.
In Rails 3, I could build an object in the view to check authorization via cancan.
<% if can? :create, @question.answers.new %>
# Code...
<% end %>
In Rails 3, the difference between .new
and .build
was that .build
added the newly built object to the parent's collection, which then resulted in an additional record in the view, which obviously was not desired.
In Rails 4, however, both add the object to the collection, rendering an empty record in the view.
Has anyone any advice on how to solve this? Checking if a record is .persisted?
in the view would be an option but somehow I feel I shouldn't have to do that.
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 can't just check by class. An actual instance is actually needed to compare based on the relation.