title: how to make this code run only before_create
body: <p>I am trying to convert my users' ips from string to integers : </p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    [:current_sign_in_ip, :last_sign_in_ip].each do |field|
      define_method(field) do
        ip = read_attribute(field)
        return nil unless ip
        ip += 4_294_967_296 if ip &lt; 0 # Convert from 2's complement
        "#{(ip &amp; 0xFF000000) &gt;&gt; 24}.#{(ip &amp; 0x00FF0000) &gt;&gt; 16}.#{(ip &amp; 0x0000FF00) &gt;&gt; 8}.#{ip &amp; 0x000000FF}"
      end
  
      define_method("#{field}=") do |value|
        quads = value.split('.')
        if quads.length == 4
          as_int = (quads[0].to_i * (2**24)) + (quads[1].to_i * (2**16)) + (quads[2].to_i * (2**8)) + quads[3].to_i
          as_int -= 4_294_967_296 if as_int &gt; 2147483647 # Convert to 2's complement
        else
          as_int = nil
        end
        write_attribute(field, as_int)
      end
  
    end
  end
  </code></pre>
  
  <p>This code works fine, it converts the string to integers and saves them. However, I want this code to run only before_create action. I tried to put it in a <code>before_create do</code> block but I got errors about <code>undefined method define_method' for #&lt;User:0x007fb97c7076b8&gt;</code> . Even if you could just direct me to the right direction that'd be great. </p>
  
  <p>Thanks</p>