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

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

    add_index :photos, :album_id

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



Rails : ActiveRecord : Enum

Active Record’s enum makes your life alot easier. If you need to maintain status/state of any activerecord object this feature could be great help.

Declare an enum attribute where the values map to integers in the database, but can be queried by name.


class Conversation < ActiveRecord::Base
  enum status: [ :active, :archived ]

# conversation.update! status: 0
conversation.active? # => true
conversation.status  # => "active"

# conversation.update! status: 1
conversation.archived? # => true
conversation.status    # => "archived"

# conversation.update! status: 1
conversation.status = "archived"

# conversation.update! status: nil
conversation.status = nil
conversation.status.nil? # => true
conversation.status      # => nil



Ruby On rails: Assign values from form to Model object but not save

Yes we can update a new Model object with values from a form saved in params without hitting to database.

# Assuming that params contains data from the form
# model_params is a private method that checks if only permitted columns
# are fetched and returns the fields as Hash

newObj = Model.find params[:id]
newObj.assign_attributes model_params

Note: update_attributes = assign_attributes + save