Ruby on Rails Monday, May 26, 2014

On May 26, 2014, at 9:03 AM, Ronald Fischer wrote:

> 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.

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

--
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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/C245783E-9001-4EFF-8B7E-39565B157E26%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment