Playing with Ohm

Today I restructured all the folders in my application migraine-app. I understand more about how it works.

I was playing with the terminal and I could understand how user information, inputs and outputs are stored in the server.

The application have of 5 sections:

  • Inputs
  • Outputs
  • Types
  • Dashboard
  • Users

I created a class called Type (The class starts always with uppercase). This class have 2 attributtes:

  • category
  • name

And it will reference a User.

class Type < Ohm::Model
  reference :user, :User

  attribute :category
  attribute :name

I created a class called User. This class have 3 atributtes:

  • name
  • email
  • password
class User < Ohm::Model
  attribute :name
  attribute :email
  attribute :password

  collection :types,   :Type
Now we can play with Types and Users:

When you use require to load a file you need yo user require_relative this is perfect for accessing to my test data:

$ cd ~/code/migraine-app/  
$ irb
require_relative "app"

If everything is correct it should show true. If you get an error because the path is wrong or it’s misspelled.

I call create on type and pass a hash of attributes category and name.

Type.create category: "Food", name: "Chocolate"

The ID es correlative unless you assign a value. If you input: Type.all.to_a
you get a full list with the types that we have created.


You can do the same with the users:

Here for example are trying something different:
If we give to the class user a metod:

Now we write:


length used to count.

We will give the number of total IDs that we have created:
In this case it is 28.

Now we are going to assign a value to a name, email and password.

Now we write:


We will give back 28. Why not 29?
This happens because we are not saving.

If we put:

Now the answer is 29