views:

28

answers:

1

Is is possible to pass options to a find_or_create method? I'd like to include a couple associations it the record is there.

I though something like this would work but it's not including them.

Event.find_or_create_by_asset_id(asset_id, :include => [:tags, :address]) 
A: 

For dynamic finders like this, the hash of options is actually ignored by the finder part of the method and instead only used by the create method (if called). This is to enable passing in attribute values to the create method e.g. {:name => "My Asset", :size => 123}

It is inconsistent behaviour compared to other ActiveRecord finders and had me stumped until I did some investigating. Here's a discussion about it on the Rails Core mailing list from a couple of years ago: http://www.mail-archive.com/[email protected]/msg05751.html

Sidane