search for: first_or_cr

Displaying 2 results from an estimated 2 matches for "first_or_cr".

2012 Jul 24
4
Behavior of first_or_create
...ied attributes as additional conditions. If nothing else, the documentation should be updated to reflect this scenario - the last case in the examples *almost* describes this scenario, but the block form used there makes it unclear what: User.where(:first_name => ''Scarlett'').first_or_create(:last_name => "O''Hara") would do. I also found that DataMapper implements the behavior I was expecting in their first_or_create method: https://github.com/datamapper/dm-core/blob/master/lib/dm-core/model.rb#L448 Thoughts? --Matt Jones -- You received this message...
2012 Mar 22
6
rescuing ActiveRecord::RecordNotUnique: clever or ugly?
...< ActiveRecord::Migration def change create_table :relations do |t| t.references :src, :null => false t.references :dst, :null => false end add_index :relations, [:src_id, :dst_id], :unique => true end end One obvious way to enforce uniqueness would be using first_or_create: def self.create_relation(src, dst) where(:src_id => src.id, :dst_id => dst.id).first_or_create! end Which works, but that always does a SELECT before creating the record. Since I don''t expect users to attempt to create duplicates very often, I''ve written a the metho...