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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/DwuTJUQ5BCAJ. For more options, visit https://groups.google.com/groups/opt_out.
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 On Wednesday, November 14, 2012 1:07:51 AM UTC+2, Stan McFarland 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? > > Thanks > > Stan McFarland > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/C2JHMCCGGccJ. For more options, visit https://groups.google.com/groups/opt_out.
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 On Tuesday, November 13, 2012 7:07:51 PM UTC-4, Stan McFarland 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? > > Thanks > > Stan McFarland > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/41cP9_R4wp8J. For more options, visit https://groups.google.com/groups/opt_out.
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 On Wednesday, November 14, 2012 5:00:07 AM UTC-5, Ace Suares wrote:> > 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 > > > > On Tuesday, November 13, 2012 7:07:51 PM UTC-4, Stan McFarland 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? >> >> Thanks >> >> Stan McFarland >> >>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/5knPaqVhkEEJ. For more options, visit https://groups.google.com/groups/opt_out.
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 On Wednesday, November 14, 2012 10:38:40 AM UTC-4, Stan McFarland wrote:> > 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 > > > > On Wednesday, November 14, 2012 5:00:07 AM UTC-5, Ace Suares wrote: >> >> 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 >> >> >> >> On Tuesday, November 13, 2012 7:07:51 PM UTC-4, Stan McFarland 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? >>> >>> Thanks >>> >>> Stan McFarland >>> >>>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8kQBiSPHvdwJ. For more options, visit https://groups.google.com/groups/opt_out.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/r3jvNx_YE60J. For more options, visit https://groups.google.com/groups/opt_out.
On 13 November 2012 23:07, Stan McFarland <stan.mcfarland-zblGgqbDkCuC8CmZemzhMw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
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 On Wednesday, November 14, 2012 6:22:17 PM UTC-4, Colin Law wrote:> > On 13 November 2012 23:07, Stan McFarland > <stan.mc...-zblGgqbDkCuC8CmZemzhMw@public.gmane.org <javascript:>> 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 >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/kbRbd3FnP5sJ. For more options, visit https://groups.google.com/groups/opt_out.