Finding a record which I had created

34 views
Skip to first unread message

Ronald Fischer

unread,
May 26, 2014, 8:39:05 AM5/26/14
to rubyonra...@googlegroups.com
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/.

Walter Lee Davis

unread,
May 26, 2014, 8:58:02 AM5/26/14
to rubyonra...@googlegroups.com
ActiveRecord already has a finder that can do all this in one line:

Dict.first_or_create(url: params[:dictname])

Assuming that's the only thing needed to create or find an existing Dict, you should be all good there.

Walter


>
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> 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.

Walter Lee Davis

unread,
May 26, 2014, 8:59:48 AM5/26/14
to rubyonra...@googlegroups.com
Sorry, meant dictname:, not url: that's my app that has URLs, not yours.

Walter

On May 26, 2014, at 8:57 AM, Walter Lee Davis wrote:

> Dict.first_or_create(url: params[:dictname])

Ronald Fischer

unread,
May 26, 2014, 9:04:19 AM5/26/14
to rubyonra...@googlegroups.com
Walter Davis wrote in post #1147113:
> ActiveRecord already has a finder that can do all this in one line:
> Assuming that's the only thing needed to create or find an existing
> Dict, you should be all good there.

No, it should not create one. If there is no suitable record, I want to
display an error message to the user. Hence, find_or_create isn't
suitable for me.

BTW, I also tried

Dict.find(:dictname => dict_name)

but this too complained that there is a hash, which can't be converted
to an integer.

Ronald

Walter Lee Davis

unread,
May 26, 2014, 9:31:32 AM5/26/14
to rubyonra...@googlegroups.com
Dict.find_by_dictname() will do that for you. If you add the bang to it, you will get a full-on error, if you don't you will get nil and no error is raised. If you are using the error to trigger your 404, then you want the bang version.

Walter

>
> Ronald
>
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/27a85af7e65236ed0636ef2941572b0a%40ruby-forum.com.

Ronald Fischer

unread,
May 26, 2014, 10:20:11 AM5/26/14
to rubyonra...@googlegroups.com
Thanks, I will try this!

I wonder: Where do I find a specification for this feature? I guess
Rails automatically generates this kind of find_by_... functions for
each attribute in my model, which as "uniqueness" set. Where is this
documented? My biggest problem with rails is getting from the API docs
the information I need.

Just for my understanding: Could you also please explain, why my
original (complicated) version failed?

Ronald

Walter Lee Davis

unread,
May 26, 2014, 11:12:04 AM5/26/14
to rubyonra...@googlegroups.com
This could be written as this:

@dict = Dict.where(:dictname => dict_name).first

It will either return a record, or nil.

Walter

On May 26, 2014, at 8:38 AM, Ronald Fischer wrote:

Ronald Fischer

unread,
May 27, 2014, 3:48:49 AM5/27/14
to rubyonra...@googlegroups.com
Walter Davis wrote in post #1147134:
> @dict = Dict.where(:dictname => dict_name).first
>
> It will either return a record, or nil.

Thanks a lot. This works perfectly well!

Ronald
Reply all
Reply to author
Forward
0 new messages