Idempotent Seeds

You Reap What You Sow rake db:seed

seeds

When working with database based applications more often than not, some seed data needs to be created. For example, I’ve used seed data to create an admin_user and a few dummy_users so the site can be used upon a fresh install. Some other common cases include things like, list of names of countries/states, list of user roles, list of permissions etc.
This post is about some ideas to manage seed data in a Ruby on Rails project.

In a Rails application seeds can be put at two places

  • db/seeds.rb [Standard practice]
  • db/migrate/232422_some_migration.rb [*its a possibility]

db/seeds.rb is the obvious place for seeds. Don’t mess around with it unless absolutely necessary.

Some points to keep in mind when sowing seeds:

  • Handling large seed data
    • Split seed files into multiple files clearly indicating your objects and business logic. A sample might look like
              |-app
                |-db
                  -seeds.rb
                  |-seeds
                    -user.rb
                    -roles.rb
                    -plans.rb
    •  Inside seeds.rb require sub seed-files as
      require 'seeds/user.rb'
      require 'seeds/roles.rb'
      require 'seeds/plans.rb'
    • If number of seeds for a business object is large, consider loading it from yaml files. Keeping business logic separate from data is a good practice.
  • Idempotent seeds
    • Idempotent,  a mathematical term that means that something remains the same when some operation is applied to it, using itself as input.
    • For example, multiplication by identity element  5 X 1 = 5.
    • Having idempotent seed files means, they can be run multiple times without causing any issue.
    • Instead of creating or updating objects blindly, it first tries to find the object and check its state.
Advertisements

2015 in review

The WordPress.com stats helper monkeys prepared a 2015 annual report for this blog.

Here’s an excerpt:

A San Francisco cable car holds 60 people. This blog was viewed about 500 times in 2015. If it were a cable car, it would take about 8 trips to carry that many people.

Click here to see the complete report.

The Psychopathology of Everyday Things

Key takeaways from the first chapter “The Psychopathology of Everyday Things” from the book The Design of Everyday Things by Don Norman

What is a good design?

  • Two of the most important characteristics of good design are discoverability and understanding.
  • Discoverability: Is it possible to even figure out what actions are possible and where and how to perform them?
  • Understanding: What does it all mean? How is the product supposed to be used? What do all the different controls and settings mean?
  • It is the duty of the machines and those who design them to understand people. It is not people’s duty to understand the arbitrary, meaningless dictates of machines.
  • Major cause of design deficiency are the engineers who are expert in technology but are limited in their understanding of people.
  • Design is a fascinating interplay of technology and psychology.

Fundamental principals of interaction

  • Affordance: It is the relationship between the properties of an object and the capabilities of the interacting agent that determine how the object can possibly be used. Example, A chair affords (is for) support therefore affords sitting.
  • Presence of affordance is jointly determined by the qualities of the object and the ability of the interacting agent.
  • Signifiers: Affordance determines what actions are possible. Signifiers communicate where the action should take place. These indicate the point of contact for an interaction.
  • Example: In case of a display with touch support, the capability to accept the touch gesture is an affordance of the display while a circle mark indicating the area to touch is a signifier. We need both.
  • Mapping: It is a term borrowed from maths, meaning the relationship between the objects of two sets. It is an important concept in design for layout of controls and displays. Use spatial correspondence between the layout of the control and the devices being controlled.
  • Feedback: It means communicating result of an action. It must be immediate and informative.
  • Conceptual Models: It is highly simplified explanation of how something works. These reside in the minds of the people who use the object.
  • For efficient usage of the object, the conceptual model object in the minds of the user should be close to the developer’s model of an object.
  • A good conceptual model allows us to predict the effects of our actions.
For details on each of the points above please read the book. Its a real good read for designers and developers alike.

 

Edit: This is what bad design can do to you.
bad design
Idea of a bad design