Question

I am new to RoR and am working on creating a database. Each host has two system admin. I am trying to associate the name given while creating a host with their respective system admin. How would I go about doing that?

If a user inputs the name of the system admin then how can I get the id using that name and insert that id into primary_sadmin_id field ?

Systemadmin.rb

class Systemadmin < ActiveRecord::Base
  attr_accessible :id, :email, :location, :name, :netid, :priphone, :secphone
  has_many :primary_sysadmin, :class_name => 'Host', :foreign_key => 'primary_sadmin_id'
  has_many :seconday_sysadmin, :class_name => 'Host', :foreign_key => 'seconary_sadmin_id'
end

host.rb

class Host < ActiveRecord::Base
  attr_accessible :iogroup, :ip, :location, :name, :opsystem, :primary_sadmin_id, :purpose, :secondary_sadmin_id, :host_type
  belongs_to :primary_sadmin, :class_name => 'Systemadmin', :foreign_key => 'primary_sadmin_id'
  belongs_to :secondary_sadmin, :class_name => 'Systemadmin', :foreign_key => 'secondary_sadmin_id'

end

host_controller.rb

def create
@host = Host.new(params[:host])
@host.primary_sadmin_id = Systemadmin.find_by_name(:sa_name1)
@host.secondary_sadmin_id = Systemadmin.find_by_name(:sa_name2)
respond_to do |format|
  if @host.save
    format.html { redirect_to @host, notice: 'Host was successfully created.' }
    format.json { render json: @host, status: :created, location: @host }
  else
    format.html { render action: "new" }
    format.json { render json: @host.errors, status: :unprocessable_entity }
  end
end
end

schema.rb

create_table "hosts", :force => true do |t|
t.string   "name"
t.integer  "ip",                  :precision => 38, :scale => 0
t.string   "location"
t.string   "host_type"
t.string   "opsystem"
t.string   "iogroup"
t.integer  "primary_sadmin_id",   :precision => 38, :scale => 0
t.integer  "secondary_sadmin_id", :precision => 38, :scale => 0
t.string   "purpose"
t.datetime "created_at",                                         :null => false
t.datetime "updated_at",                                         :null => false
end

create_table "systemadmins", :force => true do |t|
t.string   "name"
t.string   "netid"
t.integer  "priphone",   :precision => 38, :scale => 0
t.integer  "secphone",   :precision => 38, :scale => 0
t.string   "email"
t.string   "location"
t.datetime "created_at",                                :null => false
t.datetime "updated_at",                                :null => false
end
Was it helpful?

Solution

Solved it using find_or_create_by_name.

class Host  'Systemadmin', :foreign_key => 'primary_sadmin_id'
  belongs_to :secondary_sadmin, :class_name => 'Systemadmin', :foreign_key => 'secondary_sadmin_id'
  has_many   :database, :class_name => '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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top