On Tuesday, October 15, 2013 1:38:53 PM UTC-6, Jeremy Evans wrote:

> On Tuesday, October 15, 2013 10:07:53 AM UTC-7, Rodrigo Rosenfeld Rosas 
> wrote:
>>
>> Is there any alternative in Sequel for ActiveRecord's 
>> find_or_initialize_by class method?
>>
>>
>> http://edgeapi.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-find_or_initialize_by
>>
>> http://guides.rubyonrails.org/active_record_querying.html#find-or-initialize-by
>>
>> I could only find find_or_create:
>>
>>
>> http://sequel.rubyforge.org/rdoc/classes/Sequel/Model/ClassMethods.html#method-i-find_or_create
>>
>> Thanks in advance,
>> Rodrigo.
>>
>>
> Nope, there isn't an alternative method.  You can do:
>
>   object = Model.find(...) || Model.new(...)
>
> I'm not sure this method is common enough to be warrant adding to 
> Sequel::Model itself.  As it is trivial to implement yourself, there 
> doesn't seem to be a good reason to add it.
>

I've used this when implementing a shopping cart, where you don't want more 
than one of the same item in your cart.  The one liner above is what I 
used.  IMO, you can't get any simpler than that.  It's also easy to break 
into an if else end if the need arises.


-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to