Topic: Why does this code not work in the production environment?

I am hoping someone can shed some light on why I might be having a problem here.

Below is the code I wrote for my application's configuration form. It is based on Ryan Bates's 'Creating Many Models in One Form'.

http://railsforum.com/viewtopic.php?id=1063

My code allows my user to add form elements to add more internet minimums for his shop setup, etc.

The PROBLEM is my code works on my development machine. However, it fails with validation error 'Interment minimum invalid ' when I deploy this application online. I should also point out that Internet_minimums model works, but the validation messages are never passed and I only get the 'Internet minimum invalid' message.

#In my shop controller
def update   
    @mins = InternetMinimum.new   
    @shop.update_attributes( params[ :shop ] )
    @line_of_sale =  @shop.line_of_sales.find( :first, :conditions => 'type = "InternetRate"' ) ||
        @line_of_sale =  InternetRate.new
    @line_of_sale.update_attributes( params[ :line_of_sale ] )
    @line_of_sale.name = 'Internet Rate' #  default if new or old, there is only one per shop           
    @shop.all_internet_minimums.each { |min| min.destroy }
    if params[ :internet_minimums ]
        params[ :internet_minimums ].each_value do |min |                    
            @shop.internet_minimums.build(min) unless min.values.all?( &:blank? )   
        end   
    end
    Shop.transaction do
        @shop.save!
        @line_of_sale.shop = @shop
        @line_of_sale.save!
        flash[ :notice ] = 'Shop was successfully updated.'
        redirect_to :action => :index
    end       
rescue ActiveRecord::RecordInvalid => e
    @line_of_sale.valid?
    @shop.valid?
    render :action => :setup       
end

#models
class InternetMinimum < ActiveRecord::Base
    belongs_to :shop
   
    validates_presence_of :minutes, :min_charge, :message => "must be present"
    validates_uniqueness_of :minutes, :message => "must be unique", :scope => 'shop_id'
    validates_numericality_of :minutes, :only_integer => true, :message => "number only, no other characters allowed"
    validates_numericality_of :min_charge, :message => 'must be a number (decimal is OK)'
    validates_length_of :minutes, :maximum => 4   
end

#Inside my view
    <ol id="min_sets">
    <% @shop.internet_minimums.each_with_index do |mins, index| %>   
        <%= render :partial => 'set_field', :locals => { :mins => mins, :index => index } %>
    <% end %>
    <% if @shop.internet_minimums.size == 0 %>
        <%= render :partial => 'set_field', :locals => { :mins => @mins, :index => 0 } %>
    <% end %>   
    </ol>
    <%= render :partial => 'add_set_link', :locals => { :index => @shop.internet_minimums.size + 1 } %>
   
#partials
#_set_field.rhtml
<% fields_for "internet_minimums[#{index}]", mins do |ff| %>
    <li id="min_set_<%= index %>">Minutes: <%= ff.text_field :minutes, :size => 2, :maxlength => 4 %>
    Minimum Charge: <%= ff.text_field :min_charge, :size => 2 %>
     <small><%= link_to_remote 'remove', :url => { :action => 'remove_set', :index => index } %></small></li>
<% end %>

#_add_set_link.rhmtl
<p id="add_set_link">   
    <%= link_to_remote 'add minimum set',
            :url => { :action => 'add_set', :index => index } %>
</p>


My question:

Why does this code above work for me on my home development machine and then fails when it is deployed online?

Last edited by bobbyl (2007-07-28 07:29:26)

Re: Why does this code not work in the production environment?

Why would my code behave differently on live server than on my development box?

Additional information:
Server has:
Ubuntu Dapper 6.06 LTS
ruby 1.8.6 # originally had 1.8.4 here before I updated this
rails 1.2.3
Apache 2.2
mongrel 1.01


My development box:
Ubuntu Fiesty
ruby 1.8.5
rails 1.2.3
mongrel 1.01

Any chance MySQL could cause the above problem?
I will have to check, I am positive both have MYSQL 5+

Re: Why does this code not work in the production environment?

No one can help here?

Re: Why does this code not work in the production environment?

Could this problem be caused by using using Apache on the server? I only use mongrel on my ubuntu development machine?

Re: Why does this code not work in the production environment?

I am still looking for idea about how to fix this problem.

Re: Why does this code not work in the production environment?

Different version of rails between dev and production?

The main problem here is your post needs to be slimmed down to just the important lines of code where it is erroring.  Also give us a detailed explanation of the error message.  It's hard to want to help someone when there is a wall of text in the way.

Kyle Peyton - Web Developer
Santa Clara, CA - Startontop

Re: Why does this code not work in the production environment?

Hello,

did you eventually fix your problem? I'm having exactly the same, weird problem now. In development my models get saved without any problem, in production mode every possible validation fails.

Re: Why does this code not work in the production environment?

CyberMonkey,

I sorted of fixed in the round about way. It been a while, if I remember correctly...  It turned out to be a plugin or gem called Feedtools. It was using was causing problems in production mode only. I rewrote my app to not use Feedtools and above started working.

I must say I thought Feedtools was good, just beyond my coding abilities at the time to fix the problem.

Last edited by bobbyl (2008-05-05 09:15:28)

Re: Why does this code not work in the production environment?

bobbyl wrote:

Could this problem be caused by using using Apache on the server? I only use mongrel on my ubuntu development machine?

I'm having this same problem, that is, I'm using apache for production and mongrel for dev. BUT, the code that isn't working is incredibly simple. I created a new attribute for one of my models, and that new attribute works fine in dev, but in production it fails to save. I have no idea why! There isn't even any validations associated with it! Nor plugins, or anything. It's just a single variable (every other part of this model works, still, just like it did before the modifications)


-Jenny

Re: Why does this code not work in the production environment?

This seems to be a common problem, stuff works on one machine (development) and not on another (production).  My recommendation to avoid a whole lot of problems with rails and gems versions not being the same would be to freeze.  If you 'rake rails:freeze:gems' then run tests to make sure all works then deploy that whole shebang you will know you are running the same stuff in both locations.   I do not know how that integrates with capistrano or anything like that but I do know that it will eliminate a lot of variables and that is the name of the game if you are trying to do professional development.

Re: Why does this code not work in the production environment?

Often these problems are caused by migrations not having been run or failing.
There are many reasons for this problem to occur and each needs to be dealt with for their specific issues.

I would heartily recommend that a new question is raised rather than resurrecting old threads for those that have similar problems in the future allowing specific cases to be dealt with as there is no one solution fits all to this sort of issue.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Why does this code not work in the production environment?

This is why you should be doing two things. Write tests as you develop, and use a staging environment.

Writing tests helps you to isolate problems and also to fix problems with confidence as you can see what other parts of your application are affected by the changes. So look into BDD with rspec or TDD with Test::Unit, I recommend BDD with rspec, my code has become a million times better since I learned to use it.

Use a staging environment. For those who may not be familiar with this, a staging server is one that is identical to the production environment that you will deploying to. By using this you can test your code in an environment exactly like your production environment without the risk of deploying untested code to a production environment.

Between the confidence of well tested code and then being able to check your code on a staging environment before you deploy, you will become way better at what you do. Also then should you find a bug, you replicate the bug in a test/spec, write code to fix it and then know that you haven't introduced new bugs when run all your tests before you deploy again.

It seems like a lot of effort, but once you do it you'll wonder how you ever lived without it.