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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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?-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---