Find or find_by_sql: select * and more

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.

cn u show m d bit code of fields subset

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.

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

Thanks Fred, that worked.

Chris.

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.

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.