I'm new to ROR and I'm working on creating a database. Each host has two system administrators. I am trying to associate the name given during my hosting with my respective system admin.
If a user inputs the name of the system administrator, how can I get the ID using that name and enter that ID in the primary_saidmin_id field?
Systemadmin.rb host.rb host_controller.rb schema.rb Find it find_or_create_by_name.
class host & lt; ActiveRecord :: Base attr_accessible: iogroup ,: ip ,: location ,: name,: opsystem,: primary_sadmin_id ,: purpose, secondary_sadmin_id ,: host_type belongs_to: primary_sadmin ,: class_name = & gt; 'Systemadmin',: foreign_key = & gt; 'Primary_sadmin_id' is_to: secondary_sadmin ,: class_name = & gt; 'Systemadmin',: foreign_key = & gt; 'Secondary_sadmin_id' end
def make @ host = host.new (consult [: host]) @ host.promery_saidmin_id = system admin Find_by_name (: Sa_name1) @ host.Secondary_sadmin_id = Systemadmin.find_by_name (: sa_name2) response_to do | Format | If @ host.Save format.html {redirect_to @host, notice: 'Host was created successfully.' } Format.json {Render Jason: @ host, status :: created location: @ host} and format: {action: "new"} format.json {json: @ host.errors, status :: unprocessable_entity} end-end < / Code>
create_table "host",: force => Is true T | T.string "name" t.integer "ip" ,: exact => 38 ,: Scale = & gt; 0 t.string "location" t.string "host_type" t.string "opsystem" t.string "iogroup" t.integer "primary_sadmin_id",: exact => 38 ,: Scale = & gt; 0 t.integer "secondary_sadmin_id" ,: Accuracy => 38 ,: Scale = & gt; 0 t.string "Objective" t.datetime "created_at" ,: null = & gt; Incorrect t.datetime "updated_at" ,: null = & gt; False end create_table "systemadmins",: force = & gt; Is true T | T.string "name" t.string "netid" t.integer "priphone" ,: exact => 38 ,: Scale = & gt; 0 t.integer "safephone" ,: exact => 38 ,: Scale = & gt; 0 t.string "email" t.string "location" t.datetime "created_at" ,: null = & gt; Incorrect t.datetime "updated_at" ,: null = & gt;
Systemadmin ',: foreign_key = & gt; 'Primary_sadmin_id' is_to: secondary_sadmin ,: class_name = & gt; 'Systemadmin',: foreign_key = & gt; 'Secondary_sadmin_id' is -method: database,: class_name = & gt; 'Host' def sa_name1 primary_sadmin.try (: sa_name1) end DEF sa_name1 = (sa_name1) self.primary_sadmin = Systemadmin.find_or_create_by_name (sa_name1) if sa_name1.present? End def sa_name2 secondary_sadmin.try (: sa_name2) end DEF sa_name2 = (sa_name2) self.secondary_sadmin = Systemadmin.find_or_create_by_name (sa_name2) if sa_name2.present? End End
No comments:
Post a Comment