Method: Form#assoc

Defined in:
lib/make/form.rb

#assoc(column, assoc_column, assoc_model = nil) ⇒ Object

Similar to select, but association assumed and specific associated column name can be selected



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/make/form.rb', line 76

def assoc column, assoc_column, assoc_model=nil
  # titleize column
  columnName = column.titleize
  # Create for input
  input = "\n\t<label>" + columnName + "\n\t</label>\n\t<select name=\"" + @modelName + "[" + column + "]\">"
  # Remove _id and take the assocModel unless assocModel exists
  if assoc_model
    assoc_model = assoc_model.capitalize.constantize
  else 
    assoc_model = column[0...-3].capitalize.constantize
  end
  # Keep track of how many records exist in assocModel
  total = assoc_model.distinct.count('id')
  # run a while loop for as many records there are
  counter = 1
  while counter <= total do
    val = assoc_model.find(counter).attributes[assoc_column]
    input += "\n\t\t\t<option value=\"" + counter.to_s + "\">" + val.to_s + "</option>"
    counter+= 1
  end
  input += "\n\t</select>"
  @potential_keys_to_ignore.push(column)
  @formMiddle.push(input)
  return self
end