# Can I protect access to a relationship?

**URL:** <https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112>\
**Category:** Rails\
**Created:** [February 9, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112 "2006-02-09T17:54:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [February 9, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112/1 "2006-02-09T17:54:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [February 9, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112/2 "2006-02-09T21:51:08Z")

</div>

Hi,

On 09/02/06, Jeroen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @match.winner = @some\_player  
> You have several ways of preventing such behaviour:

- Write validation method for Match  
([http://rails.outertrack.com/doc/modules/ActiveRecord%3A%3AValidations#validate](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

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [February 10, 2006, 12:39am UTC](https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112/3 "2006-02-10T00:39:41Z")

</div>

I am trying to validate a record without saving it. How can that be  
accomplished?

Thanks,

Willi

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [February 10, 2006, 1:13am UTC](https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112/4 "2006-02-10T01:13:39Z")

</div>

Valid? Gives me true or false, but I need the errors associated to each  
column.

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [February 10, 2006, 1:25am UTC](https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112/5 "2006-02-10T01:25:05Z")

</div>

Once you’ve done object.valid? you can do object.errors to get the  
validation errors, can’t you?

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [February 10, 2006, 1:02am UTC](https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112/6 "2006-02-10T01:02:37Z")

</div>

use valid?

[http://api.rubyonrails.com/classes/ActiveRecord/Validations.html](http://api.rubyonrails.com/classes/ActiveRecord/Validations.html)

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [February 10, 2006, 1:39am UTC](https://www.ruby-forum.com/t/can-i-protect-access-to-a-relationship/65112/7 "2006-02-10T01:39:46Z")

</div>

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 \< removed\_email\_address@domain.invalid  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \> wrote:

Valid? Gives me true or false, but I need the errors associated to each  
column.
