jeroen
1
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
jeroen
2
Hi,
On 09/02/06, Jeroen H. [email protected] wrote:
@match.winner = @some_player
You have several ways of preventing such behaviour:
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
jeroen
3
I am trying to validate a record without saving it. How can that be
accomplished?
Thanks,
Willi
jeroen
4
Valid? Gives me true or false, but I need the errors associated to each
column.
jeroen
5
Once you’ve done object.valid? you can do object.errors to get the
validation errors, can’t you?
jeroen
6
jeroen
7
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 < [email protected]
mailto:[email protected] > wrote:
Valid? Gives me true or false, but I need the errors associated to each
column.