gers32
February 22, 2008, 10:51am
1
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.
gers32
February 22, 2008, 12:21pm
2
cn u show m d bit code of fields subset
gers32
February 25, 2008, 10:25am
3
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.
gers32
February 25, 2008, 12:29pm
4
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
gers32
February 27, 2008, 11:18am
5
Thanks Fred, that worked.
Chris.
gers32
February 29, 2008, 4:21pm
6
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.
gers32
February 27, 2008, 12:12pm
7
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.