Forum: Ruby on Rails Can I protect access to a relationship?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2006-02-09 17:54
(Received via mailing list)
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
6578456ff00ab3520b1fe684bc572b45?d=identicon&s=25 Łukasz Piestrzeniewicz (Guest)
on 2006-02-09 21:51
(Received via mailing list)
Hi,

On 09/02/06, Jeroen Houben <jeroen@terena.nl> wrote:
> @match.winner = @some_player
You have several ways of preventing such behaviour:

* Write validation method for Match
(http://rails.outertrack.com/doc/modules/ActiveReco...)
* 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
7c81d4d4728d92c58baab68eb505a7d2?d=identicon&s=25 Willi Weichselbaumer (Guest)
on 2006-02-10 00:39
(Received via mailing list)
I am trying to validate a record without saving it. How can that be
accomplished?

Thanks,

Willi
B72456baa7959f6dc22fe82024458bda?d=identicon&s=25 Eddie Hillenbrand (Guest)
on 2006-02-10 01:02
(Received via mailing list)
7c81d4d4728d92c58baab68eb505a7d2?d=identicon&s=25 Willi Weichselbaumer (Guest)
on 2006-02-10 01:13
(Received via mailing list)
Valid? Gives me true or false, but I need the errors associated to each
column.
3ec705c5dd3480c6268b72c5617e8dae?d=identicon&s=25 Michael Smedberg (Guest)
on 2006-02-10 01:25
(Received via mailing list)
Once you've done object.valid? you can do object.errors to get the
validation errors, can't you?
7c81d4d4728d92c58baab68eb505a7d2?d=identicon&s=25 Willi Weichselbaumer (Guest)
on 2006-02-10 01:39
(Received via mailing list)
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.
This topic is locked and can not be replied to.