I am working on an application that also has an Entry model / entries table. Here is my migration:
class CreateEntries < ActiveRecord::Migration def self.up create_table :entries do |t| t.string :title t.text :entry
Very similar to what you are watching.
First, the first line, declaring a class called CreateEntries , which extends ActiveRecord::Migration .
Then we declare a class method called up() . A class method, unlike an instance method, refers to a class, and not to specific objects of the class. This is the keyword " self " that calls it as a class method.
Then call create_table() and passing it two things:
The character (" :entries "), which, as mentioned above, is similar to a string literal. This tells ActiveRecord that the created table should be called. Say you typed this code manually - forget about the generators for a minute. You typed " :entries " because you know that by the agreement tables in the Rails application they are called by multiple names, and you know that the model class that connects to this table will be called Entry .
We also pass the block.
A block can be enclosed in ...
`do ... end`
or
`{ ... }`
A block can take parameters enclosed by two " | " s. In this case, the create_table method passes the object of the TableDefinition class to the TableDefinition , so to answer one of your questions, t is var holding this object. Then inside the block, we call the various methods of the TableDefinition instance.
Where did the TableDefinition object TableDefinition ? This happens in the create_table() method. It contains code that creates a new TableDefinition object and "displays" it on a block ....
Source Code ActiveRecord ...
def create_table(table_name, options = {}) table_definition = TableDefinition.new(self) table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false yield table_definition
Ethan
source share