Ruby Skill Cultivation

Source: Internet
Author: User

Build table
Activerecord::schema.defineDo drop_table: HostsIf table_exists?: Hosts Create_table: HostsDo |table| Table.column: Name,: stringEnd Drop_table:d IsksIf table_exists?:d Isks create_table:d IsksDo |table| Table.column: host_id,: Integer table.column:d Ev_name,: String Table.column: Mnt_point,: String Table.column: Mb_available,: integerEnd Drop_table: Reportsif table_exists? :reports create_table :reports do |table| table.column :d isk_id, :integer Table.column :created_at, :d atetime table.column :mb_used, :integer  Endend         

The above code was created in total: Hosts,:d Isks, and: reports three tables.

The vast majority of examples found on the network do not drop_table this sentence, I personally think the practice will be frequently tested, plus automatic deletion is the complete step.

The function here is to correspond to the migration process in rails.

5. Defining the Model

This step goes to the point where you define the object that is used in your code, the data model

Class Host <ActiveRecord::Base Has_many:d IsksEndClass Disk <ActiveRecord::Base belongs_to:host has_many  :reportsend class< Span class= "Hljs-class" > Report < ActiveRecord :: Base belongs_to :d iskend              

The object corresponds to the previously defined table one by one, which uses macros such as BELONGS_TO and Has_many to declare the contact between objects/tables. According to the dry principle, there is no need to define table fields here!

This step is the process of defining the model in rails.

6. Generating Data
Host =Host.create (: name ="Slarti") disk = Host.disks.create (:d ev_name =  "/DEV/DISK1S1", Span class= "Hljs-symbol" >:mnt_point =  :mb_available = 80 * 1024) Disk.reports.create (:mb_used = 20 *  1024x768) disk.reports.create (:mb_used = 25 *  1024x768)                 

Inserting data can be achieved by manipulating the data model defined in the previous step.

7. Search
Host.all.each do |host|  puts "*** #{host.name} ***" host.disks.each do |disk| printf "%s(%s) %d/%d\n", disk.mnt_point, disk.dev_name, disk.reports.last.mb_used, disk.mb_available endend

Ruby Skill Cultivation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.