Reputation: 5539
I have posts that I am trying to load, but only want to load those where the published_at attribute is not nil. I've tried various combinations as mentioned in the wiki docs, but keeping getting exceptions and am not sure what I'm missing. How do I tell CanCan that any user can :read posts that have published_at != nil?
Upvotes: 1
Views: 2166
Reputation: 2936
Simply think the other way around and use cannot
method instead.
# If it is NOT hidden, allow to read
can :read, Post, :hidden_at => nil
# If it is NOT published, disallow the read
cannot :read, Post, :published_at => nil
You must call the cannot
method AFTER the can
method call as explained in Ability Precedence
Upvotes: 0
Reputation: 48626
In your Ability.rb :
can :edit, Post do |post|
post.published_at.nil?
end
If you are really talking about CanCan authorization and not querying for posts :) In case you just want to query, a scope will do in your Post model:
scope :not_published, where(:published_at => nil)
EDIT: scope for NOT nil :
scope :published, where('published_at is not NULL')
Upvotes: 3
Reputation: 7070
I think that this is a bit different. Cancan is used for authorization and not so much querying an index. Would you do something like @books = Book.find(:all, :conditions => ["published_at not null"])
Once you have this configured you can check to see if the user has the ability to update/edit/create/destroy/etc the record. If you want to list them all you would go through the normal indexing of them. If you wanted to add an edit button if the user is able to modify the records then you could put a if can? :update, book
then show the edit button.
Upvotes: 1