Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <hr> <p><em>Adding</em></p> <hr> <p><strong>from</strong></p> <pre><code>departments = ["this", "that"] departments.each{|d| Department.where(:name =&gt; d).first_or_create} </code></pre> <p><strong>to</strong></p> <pre><code>departments = ["this", "that", "there", "then"] departments.each{|d| Department.where(:name =&gt; d).first_or_create} </code></pre> <p>this is a simple example,</p> <hr> <p><em>Updating/rename</em></p> <hr> <p><strong>from</strong></p> <pre><code>departments = ["this", "that", "there", "then"] departments.each{|d| Department.where(:name =&gt; d).first_or_create} </code></pre> <p><strong>to</strong></p> <pre><code>departments = ["these", "those", "there", "then"] new_names = [['these', 'this'],['those','that']] new_names.each do |new| Department.where(:name =&gt; new).group_by(&amp;:name).each do |name, depts| depts.first.update_column :name, new[0] if new[1] == name # skips validation # depts[1..-1].each(&amp;:destroy) if depts.size &gt; 1 # paranoid mode end end departments.each{|d| Department.where(:name =&gt; d).first_or_create} </code></pre> <p><strong>IMPORTANT:</strong> You need to update the elements of <code>departments</code> array else duplication will surely happen.</p> <p><em>Work around:</em> Add a validates_uniqueness_of validation or a validation of uniqueness comparing all necessary attributes BUT don't use <a href="http://guides.rubyonrails.org/active_record_validations_callbacks.html#skipping-validations" rel="nofollow">methods skipping validations</a>. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload