In rails 3.2+, you can do this :
SomeModel.some_scope.first_or_initialize
Which means you can also do :
OtherModel.some_models.first_or_initialize
I find this pretty useful, but i'd like to have a first_or_build
method on my has_many
associations, which would act like first_or_initialize
but also add a new record to the association as build
does when needed.
update for clarification : yes, i know about first_or_initialize
and first_or_create
. Thing is, first_or_initialize
does not add the initialized record to the association's target as build
does, and first_or_create
... well... creates a record, which is not the intent.
I have a solution that works, using association extensions :
class OtherModel < ActiveRecord::Base
has_many :some_models do
def first_or_build( attributes = {}, options = {}, &block )
object = first_or_initialize( attributes, options, &block )
proxy_association.add_to_target( object ) if object.new_record?
object
end
end
end
I just wonder if :
- built-in solutions to this problem already exist ?
- my implementation has flaws i do not see ?