# Find or find\_by\_sql: select \* and more

**URL:** <https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879>\
**Category:** Rails\
**Created:** [February 22, 2008, 10:51am UTC](https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879 "2008-02-22T10:51:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [February 22, 2008, 10:51am UTC](https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879/1 "2008-02-22T10:51:04Z")

</div>

Hi,

I’m trying to retrieve ALL fields from an Oracle database table, as  
well as a subset of them, under a different name.

The equivalent select statement would be:

SELECT \*, NAME\_EN AS NAME, DESC\_EN AS DESCRIPTION FROM MYTABLE;

The problem is that this isn’t allowed (Oracle-specific?). Is there a  
way I can do this using Rails’ find method?

Thanks,

Chris.

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [February 22, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879/2 "2008-02-22T12:21:58Z")

</div>

cn u show m d bit code of fields subset

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [February 25, 2008, 10:25am UTC](https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879/3 "2008-02-25T10:25:44Z")

</div>

I don’t quite understand your question… sorry. But someone suggested  
a solution that I like; assuming I could get it to work:

I created the following method in the Model:

def description  
self[:desc\_en]  
end

and in the Controller, I have:

def by\_id\_xml  
@my\_model = MyModel.find( params[:id] )  
render :xml =\> @my\_model.to\_xml  
end

Unfortunately, by\_id\_xml still returns the same XML structure, with  
all of the table’s fields, but without the new field “description”…  
Is this due to the fact that description is a method and not a class  
variable? In which case, is there a way I can tell ActiveRecord to add  
the “description” field to the model?

Thanks,

Chris.

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [February 25, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879/4 "2008-02-25T12:29:22Z")

</div>

On 25 Feb 2008, at 09:25, gers32 wrote:

> and in the Controller, I have:  
> the “description” field to the model?  
> to\_xml takes a bunch of options for that kind of thing. In particular,  
> the :methods key is an array of methods to call and include in the  
> xml, so in your case

@my\_model.to\_xml :methods =\> [:description]  
ought to do the job.

Fred

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [February 27, 2008, 11:18am UTC](https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879/5 "2008-02-27T11:18:33Z")

</div>

Thanks Fred, that worked.

Chris.

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [February 29, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879/6 "2008-02-29T16:21:58Z")

</div>

My problem is solved: I removed “AS DESCRIPTION\_I18N” from the SQL  
query, because the variable is created in to\_xml(), which expects to see  
DESC\_FR.

Thanks to Fred,

Chris.

---

<div class="post-metadata">

**Author:** ![gers32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ed8c4c/32.png) [@gers32](https://www.ruby-forum.com/u/gers32)\
**Post date:** [February 27, 2008, 12:12pm UTC](https://www.ruby-forum.com/t/find-or-find-by-sql-select-and-more/129879/7 "2008-02-27T12:12:42Z")

</div>

Now I have a new problem, derived from the one above… My Rails code  
looks like this:

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

@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 the find method…

Unfortunately, I need to restrict the number of fields in my SELECT  
statement, because the table has a large number of them.

Is this caused by an incorrect usage of :select (specifically using AS)?

Thanks,

Chris.
