# Modifying Model Output

**URL:** <https://www.ruby-forum.com/t/modifying-model-output/64010>\
**Category:** Rails\
**Created:** [June 21, 2006, 11:15am UTC](https://www.ruby-forum.com/t/modifying-model-output/64010 "2006-06-21T11:15:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 11:15am UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/1 "2006-06-21T11:15:21Z")

</div>

Hi!

I’m a very-very new Ruby-on-Rails user even if I have some experience  
with other Languages (mostly PHP, so this is my first real  
object-oriented language)

My problem, newbie-ish as it is, is the following: I have a model,  
Article, that returns Articles from the DB. Now, when a certain field of  
the model (the intro text) contains no data, I want to fill it with a  
truncated version of another field (the full text). So far, I have tried  
adding the following to app/models/article.rb but it won’t work.

def self.intro  
if self.intro == “”  
self.intro = truncate(self.text)  
self.intro  
end  
end

Any ideas why I get nothing here?

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 11:31am UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/2 "2006-06-21T11:31:57Z")

</div>

def self.intro defines a class method, not an instance method. Use def  
intro instead. Something like this may be what you need:

def intro  
value = read\_attribute(:intro)  
value.blank? truncate(text) : value  
end

-Jonathan.

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 11:44am UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/3 "2006-06-21T11:44:29Z")

</div>

Thanks for the tip, I can’t get it to work as of yet, though.

There seemed to be a syntax error in your code, even though I couldn’t  
locate it. When I rewrote it as follows, however, I only got back empty  
strings when trying to display @articles[0].intro in my view.

def intro  
value = read\_attribute(:intro)  
if value.blank?  
value = read\_attribute(:text)  
end  
value  
end

I am not sure, but could it be that the code gets executed but the  
returned value doesn’t get forwarded into @articles?

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 11:51am UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/4 "2006-06-21T11:51:21Z")

</div>

Indeed, it should have been:

def intro  
value = read\_attribute(:intro)  
value.blank? ? truncate(text) : value  
end

That should work …

-Jonathan.

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 11:59am UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/5 "2006-06-21T11:59:05Z")

</div>

> That should work …

Yes it does indeed, thanks a lot! Could you maybe point me in the right  
direction how to get the text from the DB? read\_attribute(:text) doesn’t  
work.

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 11:54am UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/6 "2006-06-21T11:54:32Z")

</div>

Actually, truncate may not be available in a model, try:

def intro  
value = read\_attribute(:intro)  
value.blank? ? text : value  
end

-Jonathan.

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 12:40pm UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/7 "2006-06-21T12:40:08Z")

</div>

It works! You, sir, are a genius!

Using read\_attribute(:contents) which is the REAL name of the table row.  
_hangs head in shame_

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 12:10pm UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/8 "2006-06-21T12:10:16Z")

</div>

Try read\_attribute(‘text’).

-Jonathan.

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [June 21, 2006, 1:52pm UTC](https://www.ruby-forum.com/t/modifying-model-output/64010/9 "2006-06-21T13:52:56Z")

</div>

Great. But I’m not a genius 😉

-Jonathan.
