I have an Array with two Target objects. How can I manage to take each
value of the objects?
p("targets size: " + @targets.length.to_s) # "targets size: 2" (OK)
p @targets # [#<Target:0x471c2f4
@attributes={"hit_sequence_desc"=>"0",
# "hit_sequence_ID"=>"1
# ", "hit_sequence_accession"=>"0"}>,
#<Target:0x471c290
# @attributes={"hit_sequenc
# e_desc"=>"3", "hit_sequence_ID"=>"3",
# "hit_sequence_accession"=>"3"}>] (OK)
p @targets[0].attribute_names # ["hit_sequence_ID",
"hit_sequence_accession",
#"hit_sequence_desc"] (OK)
p @targets[0].class # Target (OK)
Now it comes the problem:
p @targets[0].hit_sequence_ID # nil (???)
p @targets[0].hit_sequence_desc # nil (???)
p @targets[0].hit_sequence_accession # nil (???)
···
--
Posted via http://www.ruby-forum.com/.
Dunno your class but you probably want to try this
p @targets[0].attributes["hit_sequence_ID"]
robert
···
2007/7/26, Alvaro Perez <alvaro.pmartinez@gmail.com>:
I have an Array with two Target objects. How can I manage to take each
value of the objects?
p("targets size: " + @targets.length.to_s) # "targets size: 2" (OK)
p @targets # [#<Target:0x471c2f4
@attributes={"hit_sequence_desc"=>"0",
# "hit_sequence_ID"=>"1
# ", "hit_sequence_accession"=>"0"}>,
#<Target:0x471c290
# @attributes={"hit_sequenc
# e_desc"=>"3", "hit_sequence_ID"=>"3",
# "hit_sequence_accession"=>"3"}>] (OK)
p @targets[0].attribute_names # ["hit_sequence_ID",
"hit_sequence_accession",
#"hit_sequence_desc"] (OK)
p @targets[0].class # Target (OK)
Now it comes the problem:
p @targets[0].hit_sequence_ID # nil (???)
p @targets[0].hit_sequence_desc # nil (???)
p @targets[0].hit_sequence_accession # nil (???)
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
···
--
Posted via http://www.ruby-forum.com/.
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(<your query here>)
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(<your query here>)
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
···
On 7/26/07, Alvaro Perez <alvaro.pmartinez@gmail.com> 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
Hi Todd,
thank you very much for your help. First solution doesn't work (same
error) but mapping the Target class to sequence_db or hit_sequence
tables, which are involved in the join, works perfectly. I have to
retrieve data not with
targets.first.hit_sequence_ID
but with
targets.first.attributes['hit_sequence_ID']
Thank you again,
Alvaro.
Todd Benson wrote:
···
class Target < ActiveRecord::Base
set_table_name 'sequence_db'
end
targets = Target.find_by_sql(<your query here>)
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
--
Posted via http://www.ruby-forum.com/\.