Ruby on Rails Sunday, March 18, 2018



On Sunday, 11 March 2018 14:36:30 UTC, Walter Lee Davis wrote:

.........................................
 
Here's a multi-line block format migration method:

    create_table :friendly_id_slugs do |t|
      t.string   :slug,           :null => false
      t.integer  :sluggable_id,   :null => false
      t.string   :sluggable_type, :limit => 50
      t.string   :scope
      t.datetime :created_at
    end

So you have the method create_table(), which is sent one to two arguments: the table name to be created, and a hash of options, and then optionally passed a block with the details of the table. "t" is the name you gave the block, just the same as "something" in my example. The name itself is not significant, but off of that block hangs the rest of the details for create_table to use when making your table. If you don't pass the block, then the table is created without any columns.



Thanks.

Isn't  't'  a formal parameter for/of the block,  rather than a name of the block?   I have read that blocks are anonymous/nameless.

The main thing that had me puzzled but I think I can see better now, were lines like   t.integer :abc    But I see now that integer is a method that makes a column of type integer and takes a symbol as parameter.  And the reason why it's named in such a way that it doesn't look like a verb, / doesn't look like a method, is because Active Record  uses a DSL (Domain Specific Language),  which is still Ruby but designed to not look like normal Ruby even though it is

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/be679b58-140b-4091-b70e-244a5215b07e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment