# Question about model attributes

**URL:** <https://www.ruby-forum.com/t/question-about-model-attributes/126449>\
**Category:** Rails\
**Created:** [January 18, 2008, 10:56am UTC](https://www.ruby-forum.com/t/question-about-model-attributes/126449 "2008-01-18T10:56:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [January 18, 2008, 10:56am UTC](https://www.ruby-forum.com/t/question-about-model-attributes/126449/1 "2008-01-18T10:56:12Z")

</div>

Hi,

At what point in rails are the methods for a models attributes defined?

Because I have the following code:  
[Parked at Loopia](http://pastie.caboo.se/140435)

And it returns in irb:

> > s = Playlist.find(:first).songs  
> > false  
> > 1

If I do this in irb: (The PlaylistItem model has an attribute ‘position’

> > s = Playlist.find(:first).playlist\_items[0].methods(true).include?(‘position’)  
> > true

How’s that possible? Does irb do some fancy trick to retrieve all  
methods?  
Must I do something in the instance\_eval to make the methods appear?

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [January 18, 2008, 11:21am UTC](https://www.ruby-forum.com/t/question-about-model-attributes/126449/2 "2008-01-18T11:21:58Z")

</div>

On 18 Jan 2008, at 09:56, Leon B. wrote:

> Hi,
> 
> At what point in rails are the methods for a models attributes  
> defined?
> 
> Because I have the following code:  
> [Parked at Loopia](http://pastie.caboo.se/140435)

> How’s that possible? Does irb do some fancy trick to retrieve all  
> methods?  
> Must I do something in the instance\_eval to make the methods appear?

They get generated when they are needed (via method\_missing). Why do  
you care about whether the methods exists rather than just going ahead  
and calling them ?

Fred

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [January 18, 2008, 11:30am UTC](https://www.ruby-forum.com/t/question-about-model-attributes/126449/3 "2008-01-18T11:30:38Z")

</div>

Because the actual script is something like:

association\_through\_item.methods(true).each do |method|  
#No method position here!

association\_through\_item.send(association\_method).meta\_eval do

```
define_method method do
  association_through_item.send(method)
end

```

end  
end

So position never gets called because it’s not in methods()

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [January 18, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/question-about-model-attributes/126449/4 "2008-01-18T13:46:59Z")

</div>

Thanks Fred! Worked like a charm.

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [January 18, 2008, 12:03pm UTC](https://www.ruby-forum.com/t/question-about-model-attributes/126449/5 "2008-01-18T12:03:35Z")

</div>

On 18 Jan 2008, at 10:30, Leon B. wrote:

> end  
> end  
> end
> 
> So position never gets called because it’s not in methods()  
> You could try calling PlayListItem.define\_attribute\_methods

Fred
