# Make virtual attribute more "real"

**URL:** <https://www.ruby-forum.com/t/make-virtual-attribute-more-real/170125>\
**Category:** Rails\
**Created:** [June 12, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/make-virtual-attribute-more-real/170125 "2009-06-12T16:58:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felix\_SchSSS\_fer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ed8c4c/32.png) [@Felix\_SchSSS\_fer](https://www.ruby-forum.com/u/Felix_SchSSS_fer)\
**Post date:** [June 12, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/make-virtual-attribute-more-real/170125/1 "2009-06-12T16:58:06Z")

</div>

Hello,

I have tried to tackle this one with some people on IRC, but the  
proposed solution doesn’t really satisfy me, as it solves the problem  
only in a special case, and not in general.

So I have two models in my rails app, say Child and Parent, where the  
Parent has\_many :children and the Child belongs\_to :parent. I have  
attributes I want the Child to have that the Parent holds, some exist  
as columns in the Child that if set override, some others that only  
exist in the Parent. That is realised through custom getters defined  
thus:

“”"  
%w{ some nifty attr\_ibutes }.each do |attrib|  
define\_method attrib do  
read\_attribute("#{attrib}") || parent.send("#{attrib}")  
end  
end  
“”"

So far so good, and everything there works very well.

My problem here is that attributes the Parent has the Child doesn’t  
don’t get included in stuff like .to\_xml or even .attributes. In the  
example at hand, if child has the column ‘nifty’ but not ‘some’ and  
the parent has both, Child.find(0).attributes yields ‘nifty’ but not  
‘some’, same for Child.find(0).to\_xml and so on. Is there a way to  
tell the Child model that even if the attributes are virtual, I want  
some of them to behave like real ones?

Regards,

Felix

---

<div class="post-metadata">

**Author:** ![Felix\_SchSSS\_fer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ed8c4c/32.png) [@Felix\_SchSSS\_fer](https://www.ruby-forum.com/u/Felix_SchSSS_fer)\
**Post date:** [June 13, 2009, 1:16am UTC](https://www.ruby-forum.com/t/make-virtual-attribute-more-real/170125/2 "2009-06-13T01:16:01Z")

</div>

Felix SchÃ¤fer wrote:

> My problem here is that attributes the Parent has the Child doesn’t  
> don’t get included in stuff like .to\_xml or even .attributes. In the  
> example at hand, if child has the column ‘nifty’ but not ‘some’ and  
> the parent has both, Child.find(0).attributes yields ‘nifty’ but not  
> ‘some’, same for Child.find(0).to\_xml and so on. Is there a way to  
> tell the Child model that even if the attributes are virtual, I want  
> some of them to behave like real ones?

[http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html](http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html)

## From the bottom of the above page…

## You may override the to\_xml method in your ActiveRecord::Base subclasses if you need to. The general form of doing this is class IHaveMyOwnXML \< ActiveRecord::Base def to\_xml(options = {}) options[:indent] ||= 2 xml = options[:builder] ||= Builder::XmlMarkup.new(:indent =\> options[:indent]) xml.instruct! unless options[:skip\_instruct] xml.level\_one do xml.tag!(:second\_level, ‘content’) end end end

---

<div class="post-metadata">

**Author:** ![Felix\_SchSSS\_fer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ed8c4c/32.png) [@Felix\_SchSSS\_fer](https://www.ruby-forum.com/u/Felix_SchSSS_fer)\
**Post date:** [June 13, 2009, 1:36am UTC](https://www.ruby-forum.com/t/make-virtual-attribute-more-real/170125/3 "2009-06-13T01:36:57Z")

</div>

Am 13.06.2009 um 01:16 schrieb Robert W.:

> [http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html](http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html)
> 
> From the bottom of the above page…

Yeah, I had read that one already, and that was what I meant with a  
solution for this special case: it solves the problem for to\_xml, but  
if I ever were to use to\_yaml, I’d have to override it too, and  
whatever method yields a formatted output of the model’s instance.  
What I’d really like is to make the virtual fact appear real. Another  
quick way that came to mind would be to just add the attributes as  
columns in a migration, and leave them blank, but that makes for empty  
columns and seems more of a hack than a solution.

Regards,

Felix

---

<div class="post-metadata">

**Author:** ![Felix\_SchSSS\_fer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ed8c4c/32.png) [@Felix\_SchSSS\_fer](https://www.ruby-forum.com/u/Felix_SchSSS_fer)\
**Post date:** [June 15, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/make-virtual-attribute-more-real/170125/4 "2009-06-15T15:55:00Z")

</div>

Am 13.06.2009 um 01:36 schrieb Felix Schäfer:

> whatever method yields a formatted output of the model’s instance.  
> What I’d really like is to make the virtual fact appear real. Another  
> quick way that came to mind would be to just add the attributes as  
> columns in a migration, and leave them blank, but that makes for empty  
> columns and seems more of a hack than a solution.

As a follow-up on this one: the (dirty) hack that consists in adding a  
column that basically never gets used does work, but I found a third  
way apart from that and tackling .to\_xml. In essence, you can  
pass .to\_xml some parameters to control the output of to\_xml, notably  
in this case the :methods parameter, which just goes and includes the  
return value of said methods as attributes named after these methods  
in the resulting xml. While not ideal in the sense that would have  
like it, it seems less deep-going than defining a custom .to\_xml.

Regards,

Felix
