[Rails Heroku] Problem with saving object (on heroku hosting)

Hi All,

I have some strange problem which appears only on heroku hosting 2.3.5
default stack (not on my local computer)
I have some models. Here they are:

class Contact < ActiveRecord::Base
  belongs_to :user
  belongs_to :type, :class_name => "ContactType", :foreign_key =>
"type_id"

  validates_presence_of :name, :on => :create, :message => "can't be
blank"
  validates_presence_of :type, :on => :create, :message => "can't be
blank"
  validates_presence_of :number, :on => :create, :message => "can't be
blank"
end

class ContactType < ActiveRecord::Base
  validates_presence_of :name
  validates_uniqueness_of :name
end

migration:
    create_table :contacts, :force => true do |t|
      t.integer :user_id
      t.integer :type_id
      t.string :name
      t.string :number

      t.timestamps
    end
    add_index :contacts, :user_id

    create_table :contact_types, :force => true do |t|
      t.string :name

      t.timestamps
    end
    end

And a simple form:
<% form_for(@contact) do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %>
    <%= f.text_field :name %>
  </p>

  <p>
    <%= f.label :type_id %>
    <%= f.collection_select(:type_id, @contact_types, :id, :name) %>
  </p>

  <p>
    <%= f.label :number %>
    <%= f.text_field :number %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

If i'm trying to save model and I get
There were problems with the following fields:
Contact can't be blank

What does it mean {model name} can't be blank?
When I try it local all save well.

Great Thanks.

Hi All,

I have some strange problem which appears only on heroku hosting 2.3.5
default stack (not on my local computer)
I have some models. Here they are:

class Contact < ActiveRecord::Base
belongs_to :user
belongs_to :type, :class_name => "ContactType", :foreign_key =>
"type_id"

Your problem may lie here - the type attribute (if present) is used for single table inheritance. Obviously this is adding a type method rather than an attribute but I would definitely try changing that association name.

Fred

Thanks Frederick, but I still do not understand what's going on here.

I changed this line to belongs_to :contact_type ..
The result is the same.

Controller was generated as standart scaffold.
def create
  @contact = Contact.new(params[:contact].merge(:user =>
current_user))
  respond_to do |format|
    if(@contact.save)
    ...

Contact can't be blank
Do you know when this validation should appear?

Thanks Frederick, but I still do not understand what's going on here.

I changed this line to belongs_to :contact_type ..
The result is the same.

Did you also change the validates_presence_of :type ?

Fred

Yes of course, currently it looks like

  validates_presence_of :contact_type_id
  validates_associated :contact_type

I insert some debug on the view and get something: please look into
it :slight_smile:

<% form_for(@contact) do |f| %>
  <%= debug(@contact) %>

The difference here is under

changed_attributes:
  name:
  contact_type_id:
  number:
contact:

what is attribyte attribute: :contact ?

result:
heroku:

--- &id002 !ruby/object:Contact
attributes:
  name: "44"
  updated_at:
  contact_type_id: "1"
  number: rrr
  user_id:
  created_at:
attributes_cache: {}

changed_attributes:
  name:
  contact_type_id:
  number:
contact:
contact_type: &id001 !ruby/object:ContactType
  attributes:
    name: sms
    updated_at: 2010-06-08 10:57:27.197988
    id: "1"
    created_at: 2010-06-08 10:57:27.197988
  attributes_cache: {}

  errors: !ruby/object:ActiveRecord::Errors
    base: *id001
    errors: !map:ActiveSupport::OrderedHash {}

errors: !ruby/object:ActiveRecord::Errors
  base: *id002
  errors: !map:ActiveSupport::OrderedHash
    contact:
    - !ruby/object:ActiveRecord::Error
      attribute: :contact
      base: *id002
      message: :blank
      options: {}

      type: :blank
new_record: true
user:

local:
--- &id002 !ruby/object:Contact
        attributes:
          number: ""
          name: "444"
          created_at:
          contact_type_id: "1"
          updated_at:
          user_id:
        attributes_cache: {}

        changed_attributes:
          name:
          number:
          contact_type_id:
        contact_type: &id001 !ruby/object:ContactType
          attributes:
            name: sms
            created_at: 2010-06-08 11:02:03
            updated_at: 2010-06-08 11:02:03
            id: "1"
          attributes_cache: {}

          errors: !ruby/object:ActiveRecord::Errors
            base: *id001
            errors: !map:ActiveSupport::OrderedHash {}

        errors: !ruby/object:ActiveRecord::Errors
          base: *id002
          errors: !map:ActiveSupport::OrderedHash
            number:
            - !ruby/object:ActiveRecord::Error
              attribute: :number
              base: *id002
              message: can't be blank
              options: {}

              type: :blank
        new_record: true
        user:

Here is some additional notes:
If anybody know please explain:

class Contact < ActiveRecord::Base
  belongs_to :user
  belongs_to :contact_type, :class_name => "ContactType", :foreign_key
=> "contact_type_id"

  has_many :request_contacts, :dependent => :destroy
  has_many :requests, :through => :request_properties

  validates_presence_of :name, :on => :create, :message => "can't be
blank"
  validates_presence_of :type, :on => :create, :message => "can't be
blank"
  validates_presence_of :number, :on => :create, :message => "can't be
blank"

  validates_presence_of :contact_type_id
  validates_associated :contact_type
end

Full model class

If I run script/console on heroku and localhost and make some
commands:

@c = Contact.new
@c.methods.grep(/cont/).sort

on heroku
=> ["autosave_associated_records_for_contact",
"autosave_associated_records_for_contact_type",
"autosave_associated_records_for_request_contacts", "build_contact",
"build_contact_type", "contact", "contact=", "contact_type",
"contact_type=", "contact_type_id", "contact_type_id=",
"contact_type_id?", "create_contact", "create_contact_type",
"has_many_dependent_destroy_for_request_contacts", "loaded_contact?",
"loaded_contact_type?", "request_contact_ids", "request_contact_ids=",
"request_contacts", "request_contacts=", "set_contact_target",
"set_contact_type_target",
"validate_associated_records_for_request_contacts"]

localhost
=> ["autosave_associated_records_for_contact_type",
"autosave_associated_records_for_request_contacts",
"build_contact_type", "contact_type", "contact_type=",
"create_contact_type",
"has_many_dependent_destroy_for_request_contacts",
"loaded_contact_type?", "request_contact_ids", "request_contact_ids=",
"request_contacts", "request_contacts=", "set_contact_type_target",
"validate_associated_records_for_request_contacts"]

There are different numbers of methods. Why? What are the methods
contact and contact= for Contact class?

Are you re-opening the Contact class anywhere? Is the difference local
versus heroku or development versus production mode (in production
mode all your source gets loaded ahead of time which can sometimes
pull in files you wouldn't normally have loaded)

Fred

Definitely not. The project is the same. I'm just git push it. o.k
i'll dig into prod vs dev mode and try to see the difference. Anyway
thanks a lot for your attention.

Frederick thanks It was my mistake. Reopen for Contact was in my project.

I added an error model and forgot. Following code was there
request_contact.rb file
class Contact < ActiveRecord::Base
  belongs_to :request
  belongs_to :contact
..

Interesting moment here is that this reopening doesn't appear locally.
That is :contact added like a method. I guess it is difference in prod
and dev modes?

Frederick thanks It was my mistake. Reopen for Contact was in my project.

I added an error model and forgot. Following code was there
request_contact.rb file
class Contact < ActiveRecord::Base
belongs_to :request
belongs_to :contact
..

Interesting moment here is that this reopening doesn't appear locally.
That is :contact added like a method. I guess it is difference in prod
and dev modes?

It's because in production rails reads all of your app classes ahead
of time whereas in development request_contact.rb wouldn't be read
until it was needed (which may well be never if that's an obsolete
piece of code)

Fred