Possible Duplicate:
Rails primary key and object id
I am baffled as to why I cannot refer an object’s attributes; hope someone can help…
I need to build a hash that will associate Marker.marker_name with it’s id from the database, which are already stored (the record id will serve as a foreign key in another table).
So, first I retrieve the Marker record via this named scope:
class Marker < ActiveRecord::Base
named_scope :by_name, lambda { |marker_name|
{:conditions => ["marker_name = ?", marker_name]}}
which is called from my Uploads model, like this (marker_name has the value “Amelogenin”):
this_marker = Marker.by_name(marker_name)
I know this worked, because when I Use the debugger, I can see what is in this_marker, which looks like:
(rdb:2) y this_marker
- !ruby/object:Marker attributes:
created_at: 2011-03-14 22:21:27.244885
updated_at: 2011-03-14 22:21:27.244885
id: “11”
marker_name: Amelogenin attributes_cache: {}
Yet, I cannot assign the record id in my hash, like this:
$markers[marker_name] = this_marker.id
I cannot seem to refer directly to the id in this way; because, even in the debugger, I get this error:
(rdb:2) p this_marker.id
(__DELEGATION__):2: warning: Object#id will be deprecated; use Object#object_id
Is there some kind of different Ruby syntax I need to be using or what? How can I associate the marker_name with its record id?
Thanks in advance….
This
Marker.by_name(marker_name)returns anarrayof makers. You should write: