Martin
Martin

Reputation: 11336

ActiveRercord: find_or_create_by_association

I'm wondering what would be the ideal way to find or create a nested record using something similar to the find_or_crate method.

For regular attributes there's the very convenient User.find_or_create_by_attribute method.

Is there anything for a nested association?

Let's say a User has a Profile. What would be the ideal way to find or create based on its profile presence?

Upvotes: 0

Views: 62

Answers (1)

cdesrosiers
cdesrosiers

Reputation: 8892

I don't think you're going to find anything sexier than profile = user.profile || user.create_profile.

Upvotes: 1

Related Questions