# Find first or create

**URL:** <https://www.ruby-forum.com/t/find-first-or-create/185097>\
**Category:** Rails\
**Created:** [March 13, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/find-first-or-create/185097 "2010-03-13T17:14:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [March 13, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/find-first-or-create/185097/1 "2010-03-13T17:14:44Z")

</div>

Is there any ActiveRecord’s dynamic finder that can allow me to find  
first association or create it if it doesn’t exist. Something like this  
(not this code is conceptual - it does not work!):

Comment.posts.find\_or\_create(:first)

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [March 13, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/find-first-or-create/185097/2 "2010-03-13T17:16:00Z")

</div>

Milan D. wrote:

> Is there any ActiveRecord’s dynamic finder that can allow me to find  
> first association or create it if it doesn’t exist. Something like this  
> (not this code is conceptual - it does not work!):
> 
> Comment.posts.find\_or\_create(:first)

I meant:

Post.comments.find\_or\_create(:first) lol

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [March 13, 2010, 8:13pm UTC](https://www.ruby-forum.com/t/find-first-or-create/185097/3 "2010-03-13T20:13:38Z")

</div>

On 13 March 2010 16:14, Milan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Post.comments.find\_or\_create(:first)

If it doesn’t find a comment, and creates a new one, what values is it  
going to populate the record with?  
Or is it that you want a new blank record for some purpose?

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [March 13, 2010, 8:16pm UTC](https://www.ruby-forum.com/t/find-first-or-create/185097/4 "2010-03-13T20:16:05Z")

</div>

Either that or perhaps to be able to use the find\_or\_initialize  
counterpart.

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [March 13, 2010, 8:20pm UTC](https://www.ruby-forum.com/t/find-first-or-create/185097/5 "2010-03-13T20:20:59Z")

</div>

In this case I have before create hooks in place to fill in some  
attributes.

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [March 13, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/find-first-or-create/185097/6 "2010-03-13T20:43:28Z")

</div>

On 13 March 2010 19:20, Milan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In this case I have before create hooks in place to fill in some  
> attributes.

If you trim the whole of the email you’re replying too, we lose all  
sense of context…

Anyway… a simple solution is to have a method add a new comment if  
the comments array is empty, and then send the call that you pass to  
the array (so you can use any Array method as a parameter). More  
complex solutions would be to patch AR::Base or maybe Array to do a  
similar thing.

# in your Post model

def find\_or\_create\_comment(which)  
Post.comments \<\< Comment.create if Post.comments.blank?  
Post.comments.send(which.to\_s) if  
Post.comments.respond\_to?(which.to\_s)  
end

That’s off the top of my head so it is probably buggy, but might put  
you on the tail of something that suits - it relies on the “create”  
method actually working, so if the Comment fails validation, it will  
break big time.

I think better solutions might be suggested if you describe the  
problem that you have that you want to use this as a solution to…

Regards,

---

<div class="post-metadata">

**Author:** ![milandobrota](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@milandobrota](https://www.ruby-forum.com/u/milandobrota)\
**Post date:** [March 13, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/find-first-or-create/185097/7 "2010-03-13T18:50:31Z")

</div>

On Mar 13, 2010, at 11:16 AM, Milan D. wrote:

> Post.comments.find\_or\_create(:first) lol  
> Well, this is strictly off the top of my head, but could you:

@post = Post.find(…somehow…)

Comment.find\_or\_create\_by\_post\_id(:post\_id =\> @post.id)

If that doesn’t do what you want, post more code that shows what you  
want and what you’ve tries that is giving you a different result.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
