# Find() not returning value of dynamically renamed field

**URL:** <https://www.ruby-forum.com/t/find-not-returning-value-of-dynamically-renamed-field/130560>\
**Category:** Rails\
**Created:** [February 29, 2008, 11:50am UTC](https://www.ruby-forum.com/t/find-not-returning-value-of-dynamically-renamed-field/130560 "2008-02-29T11:50:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 29, 2008, 11:50am UTC](https://www.ruby-forum.com/t/find-not-returning-value-of-dynamically-renamed-field/130560/1 "2008-02-29T11:50:46Z")

</div>

Hi,

I have a database table, containing a large number of fields, among  
which a bunch of i18n fields, like DESC\_EN, DESC\_FR, DESC\_DE, etc. I  
would like to dynamically select which one of these fields I will  
return, by using SQL’s “AS” pointing to the same field name:  
DESCRIPTION\_I18N.

My Rails code looks something like this:

### MODEL

def description\_i18n

# The actual field is selected dynamically:

```
self[:desc_fr]

```

end

### CONTROLLER

@my\_data = MyTable.find(  
:all,  
:select =\> “ID, CODE, DESC\_FR AS DESCRIPTION\_I18N”,  
:conditions =\> conditions\_list)  
@my\_data\_i18n = @my\_data.to\_xml :methods =\> [:description\_i18n]  
render :xml =\> @my\_data\_i18n

##########################

The resulting XML structure lacks a value for the DESCRIPTION\_I18N  
field, unless I comment out the :select option in find()…

Unfortunately, I need the :select option to restrict the number of  
fields in my SELECT statement, otherwise I get way too much data.

Can anyone figure out what I’m doing wrong?

Thanks,

Chris.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 29, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/find-not-returning-value-of-dynamically-renamed-field/130560/2 "2008-02-29T12:20:40Z")

</div>

On 29 Feb 2008, at 10:50, Chris Gers32 wrote:

> Hi,
> 
> I have a database table, containing a large number of fields, among  
> which a bunch of i18n fields, like DESC\_EN, DESC\_FR, DESC\_DE, etc. I  
> would like to dynamically select which one of these fields I will  
> return, by using SQL’s “AS” pointing to the same field name:  
> DESCRIPTION\_I18N.

does it not like the fact that you have 2 things that are identical  
apart from the case?  
Also won’t this fail because the description\_i18n method wants the  
desc\_fr field which isn’t available with that name?

Fred

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [February 29, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/find-not-returning-value-of-dynamically-renamed-field/130560/3 "2008-02-29T16:02:37Z")

</div>

> does it not like the fact that you have 2 things that are identical  
> apart from the case?

That’s not the problem, because the case is irrelevant in SQL (at least  
as far as Oracle is concerned); this works fine with all other fields.

> Also won’t this fail because the description\_i18n method wants the  
> desc\_fr field which isn’t available with that name?

I see your point: I don’t need to mention DESCRIPTION\_I18N, because  
Rails creates it in to\_xml(). So removing “AS DESCRIPTION\_I18N” solved  
my problem.

Thanks again,

Chris.
