Best way to simulate the default value for a TEXT column using Ruby on Rails?

I am using the TEXT column in my MySQL database. As indicated in the documentation, it is not possible to set a default value for these columns.

I am currently using the following code to model this behavior:

class Data
  before_save lambda {text_column ||= ''}
end

Is there any better way, more edit / active _record to do this?

+3
source share
3 answers

If you are happy with the HTML5 solution, have you tried using the: placeholder: text_field attribute?

Also do you want to fill in a text field (which captures a small amount of text) in a column of type "text"? Did you mean text_area?

, " " , , . "Factory".

, "" ActiveRecord, ""

def self.setup(params = {})
  new(params).tap do |v|
    v.text_column = "default value"
    # other defaultings
  end
end

, setup.

+3

add_column :table_name, :column_name, :string, :default => 'your text'

-1

, :

#views/controller/new.html.erb
<%= f.text_field :column_name, :value => "default value" %>

, .

edit.html.erb, - , .

-3

All Articles