# ActiveRecord basics

**URL:** <https://www.ruby-forum.com/t/activerecord-basics/110454>\
**Category:** Rails\
**Created:** [July 26, 2007, 2:21pm UTC](https://www.ruby-forum.com/t/activerecord-basics/110454 "2007-07-26T14:21:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [July 26, 2007, 2:21pm UTC](https://www.ruby-forum.com/t/activerecord-basics/110454/1 "2007-07-26T14:21:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [July 27, 2007, 11:33am UTC](https://www.ruby-forum.com/t/activerecord-basics/110454/2 "2007-07-27T11:33:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [July 27, 2007, 11:41am UTC](https://www.ruby-forum.com/t/activerecord-basics/110454/3 "2007-07-27T11:41:34Z")

</div>

> 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/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![kabish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kabish](https://www.ruby-forum.com/u/kabish)\
**Post date:** [July 27, 2007, 11:44am UTC](https://www.ruby-forum.com/t/activerecord-basics/110454/4 "2007-07-27T11:44:03Z")

</div>

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?
