Hi, I have a class Match that looks like this (simplified) class Match < ActiveRecord::Base belongs_to :opp1, :class_name => "Player", :foreign_key => "opp1_id" belongs_to :opp2, :class_name => "Player", :foreign_key => "opp2_id" belongs_to :winner, :class_name => "Player", :foreign_key => "winner_id" end Can I prevent code from doing this? @match.winner = @some_player You can protect attributes from mass assignment, but that''s not good enough in this case.. Jeroen
Ćukasz Piestrzeniewicz
2006-Feb-09 20:50 UTC
[Rails] Can I protect access to a relationship?
Hi, On 09/02/06, Jeroen Houben <jeroen@terena.nl> wrote:> I have a class Match that looks like this (simplified) > > class Match < ActiveRecord::Base > belongs_to :opp1, :class_name => "Player", :foreign_key => "opp1_id" > belongs_to :opp2, :class_name => "Player", :foreign_key => "opp2_id" > belongs_to :winner, :class_name => "Player", :foreign_key => "winner_id" > end > > Can I prevent code from doing this? > > @match.winner = @some_playerYou have several ways of preventing such behaviour: * Write validation method for Match (http://rails.outertrack.com/doc/modules/ActiveRecord%3A%3AValidations#validate) * Overload winner= method and implement validation there (rather bad idea). * Instead of saving winner id have a enum field indicating who won (like: ''home'', ''guest'', ''draw'') and write your own accessor: class Match < ActiveRecord::Base def winner case self.who_won when ''home'' then self.opp1 when ''guest'' then self.opp2 else nil end end end -- ?ukasz Piestrzeniewicz
Willi Weichselbaumer
2006-Feb-09 23:39 UTC
[Rails] How can I force the validation of a record without saving it?
I am trying to validate a record without saving it. How can that be accomplished? Thanks, Willi
Eddie Hillenbrand
2006-Feb-10 00:02 UTC
[Rails] How can I force the validation of a record without saving it?
use valid? http://api.rubyonrails.com/classes/ActiveRecord/Validations.html On Feb 9, 2006, at 4:39 PM, Willi Weichselbaumer wrote:> I am trying to validate a record without saving it. How can that be > accomplished? > > Thanks, > > Willi > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Willi Weichselbaumer
2006-Feb-10 00:13 UTC
[Rails] How can I force the validation of a record without savingit?
Valid? Gives me true or false, but I need the errors associated to each column. -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Eddie Hillenbrand Sent: Thursday, February 09, 2006 4:02 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] How can I force the validation of a record without savingit? use valid? http://api.rubyonrails.com/classes/ActiveRecord/Validations.html On Feb 9, 2006, at 4:39 PM, Willi Weichselbaumer wrote:> I am trying to validate a record without saving it. How can that be > accomplished? > > Thanks, > > Willi > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Michael Smedberg
2006-Feb-10 00:24 UTC
[Rails] How can I force the validation of a record without savingit?
Once you''ve done object.valid? you can do object.errors to get the validation errors, can''t you? On 2/9/06, Willi Weichselbaumer <willi.weichselbaumer@mcomi.com> wrote:> > Valid? Gives me true or false, but I need the errors associated to each > column. > > > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Eddie > Hillenbrand > Sent: Thursday, February 09, 2006 4:02 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] How can I force the validation of a record without > savingit? > > use valid? > > http://api.rubyonrails.com/classes/ActiveRecord/Validations.html > > On Feb 9, 2006, at 4:39 PM, Willi Weichselbaumer wrote: > > > I am trying to validate a record without saving it. How can that be > > accomplished? > > > > Thanks, > > > > Willi > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060210/5b08c660/attachment.html
Willi Weichselbaumer
2006-Feb-10 00:39 UTC
[Rails] How can I force the validation of a record withoutsavingit?
Thanks, That actually works, I somehow missed that. Willi Subject: Re: [Rails] How can I force the validation of a record withoutsavingit? Once you''ve done object.valid? you can do object.errors to get the validation errors, can''t you? On 2/9/06, Willi Weichselbaumer < willi.weichselbaumer@mcomi.com <mailto:willi.weichselbaumer@mcomi.com> > wrote: Valid? Gives me true or false, but I need the errors associated to each column. -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org ] On Behalf Of Eddie Hillenbrand Sent: Thursday, February 09, 2006 4:02 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] How can I force the validation of a record without savingit? use valid? http://api.rubyonrails.com/classes/ActiveRecord/Validations.html On Feb 9, 2006, at 4:39 PM, Willi Weichselbaumer wrote:> I am trying to validate a record without saving it. How can that be > accomplished? > > Thanks, > > Willi > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060210/d88b0824/attachment.html