I thought this is an easy one, but: I have created a record, and later
would like to retrieve it again.
Here is an excerpt of my schema:
create_table "dicts", force: true do |t|
t.string "dictname"
...
end
My model ensures that dictname is unique:
validates :dictname,
presence: true,
length: { minimum: 3 },
format: { with: /\A\D/ },
uniqueness: true
In my controller, I tried this to fetch a Dict object, when the value
for the :dictname field is stored in variable dict_name:
if Dict.exists?(:dictname => dict_name)
logger.debug('found dictionary with name '+dict_name)
dictid=Dict.where(:dictname => dict_name).select('id').first.id
logger.debug('dictid='+dictid.inspect)
@dict=Dict.find(dictid)
However, the line to calculate dictid fails with
TypeError (can't cast Hash to integer)
which would suggest that the hash argument to the 'where' method is
unexpected. But this can't really be, can it?
So, my questions:
- Why do I get this error?
- How do I fix it?
- The whole procedure to get at my Dict record, looks unnecessarily
convoluted to me. Isn't there an easier way to do it?
--
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 unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/acd4904d49a136d008319f198800eef1%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.