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?
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
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.
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?
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?
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.