From: Todd Benson Date: 2007-07-27T00:22:08+09:00 Subject: Re: Array access On 7/26/07, Alvaro Perez wrote: > Nothing special with my class: > > class Target < 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 > > There's no targets table in the DB. i'm trying to create a custom dto > class with data from two tables: > > @targets = Target.find_by_sql("SELECT hit_sequence_accession, > hit_sequence_desc, " + > "hit_sequence_ID FROM hit_sequence " + > "JOIN sequence_db USING(sequence_db_ID) ") > > But doing: > > p @targets[0].attributes["hit_sequence_ID"] > > gets me this error messagge: > > > ActiveRecord::StatementInvalid in SearchController#do_search_targets > > Mysql::Error: #42S02Table 'srnadb_chlamy_dev_rails.targets' doesn't > exist: SHOW FIELDS FROM targets Hmm, no doubt you would receive some good help from activerecord gurus on the rails list. A couple of thoughts... An ActiveRecord::Base object needs to have a relation (table) in the database because many of its methods query the database (AFAIK). Your instance variables of Target that you set with attr_accessor have nothing to do with the @attributes variable provided by ActiveRecord::Base. In fact, you broke the accessor methods Base provides when you created your instance variables, which is why you got nil with @targets[0].hit_sequence_ID. The object was returning the instance variable and not executing the accessor _method_. If you don't specifically need a homemade object to hold your queried data, you can just... targets = ActiveRecord::Base.find_by_sql() I have no idea of your database schema, but you could do this if you want a Target object... class Target < ActiveRecord::Base set_table_name 'sequence_db' end targets = Target.find_by_sql() targets.first.hit_sequence_ID But, that is hardly the activerecord way. You should really specify your Base class relations with has_many, belongs_to, etc. Todd