ryanb/cancan — Issue #507: Defining  methods in ability.rb
Author: twestonkendall
Created: 2011-11-07 21:11:55+00:00
State: open

I can't seem to make the :create Blend dependent upon if the user is on the roaster table for that specific roastery. it works fine for the :update because the Blend has a roastery associated with it after it is created.

```ruby
class Ability
include CanCan::Ability
def initialize(user)
# guest
can :read, [Farm, Roastery]
can :create, User
# Site Admin
if user && user.admin?
can :manage, :all
end
# General user
if user
can :manage, User, :id => user.id
can :read, [Roast, Bean, Batch, Blend, Check]
can :create, Blend do |blend|
blend.batch.roast.roastery.is_roaster?(user)#doesn't work because there is no roastery.
end
can :update, Blend do |blend|
blend.batch.roast.roastery.is_roaster?(user)
end
end
end
end
```

i can check the ability on the show page with

```ruby
<% if @roastery.is_roaster?(current_user) %>
```
and roastery.rb

```ruby
def is_roaster? (user)
not allowed_to_roast(user).nil?
end

def allowed_to_roast (user)
roasters.find_by_user_id(user)
end
```

the link that creates the blend looks like this

```ruby
<%= link_to 'Add a Bean', new_blend_path(:roast_id => @roast.id, :batch_id => @batch.id) %>
```
```ruby
Roastery has_many Roasts
Roasts has_many Batches
Batches has_many Blends
```

How do I gain access to the Roastery instance that the Blend will be attached to?