Question about model attributes

Hi,

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

Because I have the following code:
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?

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:
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

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()

Thanks Fred! Worked like a charm.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs