Hi all,
I’d want to create what in Java is called a Custom DTO: a model class
that wraps selected data from one or more tables.
What I do is creating first a subclass of ActiveRercord::Base
class Targt < ActiveRecord::Base
attr_accessor :hit_sequence_accession,
:hit_sequence_desc,
:hit_sequence_ID
def initialize(hit_sequence_accession,
hit_sequence_desc,
hit_sequence_ID)
@hit_sequence_accession = hit_sequence_accession
@hit_sequence_desc = hit_sequence_desc
@hit_sequence_ID = hit_sequence_ID
end
end
And then in a controller action:
@targts = Targt.find_by_sql("SELECT hit_sequence_accession,
hit_sequence_desc, " +
"hit_sequence_ID FROM hit_sequence " +
"JOIN sequence_db USING(sequence_db_ID) ")
If I don’t do Targt class a subclass of ActiveRecord::Base, I cannot use
the find_by_sql method, while if i declare it as a subclass of
ActiveRecord, Rails will think that there is a table in the database
called targts (which do not exists). If I try to retrieve data, it works
fine, but if I try to do something like this:
p @targts[0].attributes[‘hit_sequence_ID’]
will end with a:
ActiveRecord::StatementInvalid in SearchController#do_search_targets
Mysql::Error: #42S02Table ‘srnadb_chlamy_dev_rails.targts’ doesn’t
exist: SHOW FIELDS FROM targts
Any idea on how to do this??
Thanks, Alvaro.