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.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.
on 2007-07-26 14:21
on 2007-07-27 11:33
If I map the model class to one of the two databases involved in the join: >set_table_name 'hit_sequence' I can successfully retrieve data with: >targts.first.attributes['name_of_attribute'] but >targt.first.name_of_the_attribute still gets a nil why?? what's the difference?
on 2007-07-27 11:41
> I can successfully retrieve data with: > >targts.first.attributes['name_of_attribute'] > but > >targt.first.name_of_the_attribute > > still gets a nil > > why?? what's the difference? missing 's' ? Regards, Rimantas -- http://rimantas.com/
on 2007-07-27 11:44
No sorry, typing error. Both with 's': >> I can successfully retrieve data with: >> >targts.first.attributes['name_of_attribute'] >> but >> >targts.first.name_of_the_attribute >> >> still gets a nil >> >> why?? what's the difference?