# Tweak relationships & AR loading question

**URL:** <https://www.ruby-forum.com/t/tweak-relationships-ar-loading-question/89519>\
**Category:** Rails\
**Created:** [March 20, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/tweak-relationships-ar-loading-question/89519 "2007-03-20T23:58:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 20, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/tweak-relationships-ar-loading-question/89519/1 "2007-03-20T23:58:48Z")

</div>

Hi,

Imagine you have a model Guy. Pick a guy from your table.  
Maybe he is married and have children (single guys can’t have children,  
can they?). So your are tempted to write:

class Guy \< ActiveRecord::Base  
has\_many :children  
end

But if he is single, he can’t have children. So you wan’t to write:

class Guy \< ActiveRecord::Base  
if self.single  
has\_many :children  
else  
has\_one :child  
end  
end

This could work: in fact has\_\* is just a macro for a bunch of specific  
define\_method. I am wrong?

But it doesn’t: the loading of the class fails claiming for  
NoMethodError: undefined method `single’  
I don’t understand: does AR loads relationships before defining  
accessors? And if I force attr\_accessor :single, assuming AR will  
override it, I’ve got the same error. Does he parses the whole class  
seeking for relationship first?

Thanks for your help,

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’

---

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 21, 2007, 11:52am UTC](https://www.ruby-forum.com/t/tweak-relationships-ar-loading-question/89519/2 "2007-03-21T11:52:11Z")

</div>

On Tue, Mar 20, 2007 at 11:57:32PM +0100, Pierre-Alexandre M. wrote :

> But it doesn’t: the loading of the class fails claiming for  
> NoMethodError: undefined method `single’  
> I don’t understand: does AR loads relationships before defining  
> accessors? And if I force attr\_accessor :single, assuming AR will  
> override it, I’ve got the same error. Does he parses the whole class  
> seeking for relationship first?

After going deeper in the source code, @attributes =  
attributes\_from\_column\_definition is called in the initialize method of  
ActiveRecord::Base.

Actually, during the loading of the class, there is first:

ActiveRecord::Base.class\_eval do  
include ActiveRecord::Validations  
…/…  
include ActiveRecord::Associations  
…/…  
end

@attributes is not initialized yet.  
I thought ActiveRecord::Validations could help me, to see how it handles  
passed attributes, but I didn’t understand the validates\_each method  
(line 291). What’s the send method? 😕

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’
