Forum: Ruby on Rails Cancan question - create rule issue

Posted by Stan McFarland (Guest)
on 2012-11-14 00:09
(Received via mailing list)
Hi,  RoR newbie here.  Fairly new, anyway. :). I have a cancan question 
I'm hoping someone can help me with.  I have two models - 
ProposalRequest and Proposal.    Each ProposalRequest can have many 
Proposals, but a given user can submit only one Proposal for each 
ProposalRequest.  I can't figure out how to define the rule in 
ability.rb for the create action.   Can someone help?

Thanks

Stan McFarland
Posted by Maxim Shytikov (Guest)
on 2012-11-14 10:40
(Received via mailing list)
Hi Stan,

Try something like this

def initialize(user, proposal_request_id)
  can :create, Proposal unless Proposal.exists?(:proposal_request_id =>
proposal_request_id, :user_id => user.id)
end
Posted by Ace S. (ace_s)
on 2012-11-14 11:01
(Received via mailing list)
I assume you would need to know if

proposal_request.proposals.collect { |p| p.user_id }.include? user.id

so maybe you want something in the ProposalRequest like

def proposed?(user)
  proposals.collect { |p| p.user_id }.include? user.id
end

Now I assume that upon creating a Proposal, it already has a belongs_to
relation with the current PorposalRequest, but I am not quite sure of 
it.
can :create, Proposal |proposal| do
  proposal.proposal_request.proposed? user
end

Cheers
ace
Posted by Stan McFarland (Guest)
on 2012-11-14 15:40
(Received via mailing list)
Hmmm.... this kinda makes sense to me, only it didn't work.  The 
proposed?
method works correctly, but the can? method in my template returns true 
for
both proposed and non-proposed ProposalRequests.   I'm having a hard 
time
getting my head around validating a Proposal against it's parent when 
the
Proposal hasn't even been created yet.

Thanks,

Stan
Posted by Ace S. (ace_s)
on 2012-11-14 20:41
(Received via mailing list)
Exactly. The Proposal hasn't been created, but you want to create a
proposal for a certain proposal request. That PropsalRequest must 
already
exist, right? Don't know how to get that into the ability model, maybe 
like
Max did and include it in the init method?

ace
Posted by Stan McFarland (Guest)
on 2012-11-14 22:56
(Received via mailing list)
I'll try.  I'm not familiar with passing additional parameters to the 
initialize method but I'll give it a shot.  Thanks for both of your 
help.

Stan
Posted by Colin Law (Guest)
on 2012-11-14 23:22
(Received via mailing list)
On 13 November 2012 23:07, Stan McFarland
<stan.mcfarland@blackoakweb.com> wrote:
> Hi,  RoR newbie here.  Fairly new, anyway. :). I have a cancan question I'm 
hoping someone can help me with.  I have two models - ProposalRequest and 
Proposal.    Each ProposalRequest can have many Proposals, but a given user can 
submit only one Proposal for each ProposalRequest.  I can't figure out how to 
define the rule in ability.rb for the create action.   Can someone help?

It might be easier to do it with a validation rather than cancan, or
would that not do what you want?

Colin
Posted by Ace S. (ace_s)
on 2012-11-15 12:15
(Received via mailing list)
I Think a validation will only work after data is entered. With
authorization (as in CanCan) the possibility of creating a new proposal 
can
be avoided all together.

I did some experiments but yes, it's possible to put extra arguments 
into
the initialize method.

For instance, initialize(user, project_proposal=nil).

In case you want to check you could use
ability=Ability.new(user,project_proposal)
ability.can? :create, :proposal

should give you a true or false based on what project_proposal was 
entered.

Ace
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.