Forum: Ruby Array access

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alvaro P. (Guest)
on 2007-07-26 13:59
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 (???)
Robert K. (Guest)
on 2007-07-26 15:07
(Received via mailing list)
2007/7/26, Alvaro P. <removed_email_address@domain.invalid>:
>                   # ", "hit_sequence_accession"=>"0"}>,
>
>   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 (???)

Dunno your class but you probably want to try this

p @targets[0].attributes["hit_sequence_ID"]

robert
Alvaro P. (Guest)
on 2007-07-26 15:22
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
Todd B. (Guest)
on 2007-07-26 19:24
(Received via mailing list)
On 7/26/07, Alvaro P. <removed_email_address@domain.invalid> wrote:
>                hit_sequence_ID)
> @targets = Target.find_by_sql("SELECT hit_sequence_accession,
>
> 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(<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
Alvaro P. (Guest)
on 2007-07-27 13:19
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 B. 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
This topic is locked and can not be replied to.