Rails: create objects with `has_one` relation and skip validation

I encountered with this situation today;

I have `spree_user` object which can have one `bill_address` but bill address has one filed called `zipcode` field which is required and needs to be valid but I might not always have valid data for that field.

So needed to skip the validation for create method. Found that its not possible to skip validation using `create` method.  Continue reading

Passing Modifiers :: rails generate migration

2.3 Passing Modifiers

Some commonly used type modifiers can be passed directly on the command line. They are enclosed by curly braces and follow the field type:

For instance, running:

$ bin/rails generate migration AddDetailsToProducts 'price:decimal{5,2}' supplier:references{polymorphic}

will produce a migration that looks like this

class AddDetailsToProducts < ActiveRecord::Migration
  def change
    add_column :products, :price, :decimal, precision: 5, scale: 2
    add_reference :products, :supplier, polymorphic: true, index: true
  end
end

Have a look at the generators help output for further details.

Advanced usage

Sometimes you have to automatically add index for columns in your migration. It’s not a problem:

$ rails g model user email:index location_id:integer:index

Or uniq index:

$ rails g model user pseudo:string:uniq

Set limit for field of integer, string, text and binary fields:

$ rails generate model user pseudo:string{30}

Special syntax to generate decimal field with scale and precision:

$ rails generate model product 'price:decimal{10,2}'

Pay attention that you have to wrap parameter price:decimal{10,2} to quotes. It’s vital and you may have incorrect behavior of generator if you don’t do it. Full explanation of this case is here.

You can combine any single curly brace option with the index options:

$ rails generate model user username:string{30}:uniq

And the last useful feature of generators – it’s options to generate reference columns (fields which are used in rails as foreign keys):

$ rails generate model photo album:references

This command will generate photos table with integer field album_id and also it will add index for this field automatically. Make sure in it by looking at generated migration:

class CreatePhotos < ActiveRecord::Migration
  def change
    create_table :photos do |t|
      t.references :album

      t.timestamps
    end
    add_index :photos, :album_id
  end
end

For polymorphic reference use this syntax:

$ rails generate model product supplier:references{polymorphic}

Polymorphic reference with indexes:

$ rails generate model product supplier:references{polymorphic}:index

 Sources

http://railsguides.net/advanced-rails-model-generators/

Ruby On Rails : ActiveRecord : Batch update/create : Updating/Creating Multiple records at the same time

Sometime you may require to update multiple rows in your tables with different data. If you were looking for updating all record with same data then you should go for update_all method.

Batch update

 # Updating multiple records; different col with different data:
  people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
  Person.update(people.keys, people.values)

Continue reading

Ruby on Rails : Where not to use find_each

If you have arranged rows of data then you dont use find_each in views

@alerts = @user.alerts.order(updated_at: :desc)

# In views

<% @alerts.find_each do |alert| %
# This will pick data as in default order
# This may throw some Warning like
# Scoped order and limit are ignored, it's forced to be batch order and batch size

so, this should be like this

@alerts = @user.alerts.order(updated_at: :desc)

# In views

<% @alerts.each do |alert| %>
  <h2> Alert </h2>

<% end %>