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