开发者

Getting random nil.each error in mysql2 gem when using :include in Rails's find

开发者 https://www.devze.com 2023-02-16 06:27 出处:网络
I\'m randomly (as it seems) getting a nil.each error on my localhost. Was feeling pretty confident in the code though so I pushed it to heroku and it works fine there.

I'm randomly (as it seems) getting a nil.each error on my localhost. Was feeling pretty confident in the code though so I pushed it to heroku and it works fine there.

Here's my view code:

<h2><%=@book.name.upcase%></h2>

<br />

<% @book.chapters.each do |chapter| %>
    <h3>Chapter <%=chapter.number%></h3>
    <% chapter.verses.each do |verse| %>
        <b><%=verse.number%>)</b> <%=verse.body%>
    <% end %>
    <br /><br />
<% end %>

At first all I had in my controller was something like @book = Book.find(params[:id]) and that worked fine except for the speed. Then I changed to this:

def show
    if params[:book_name]
        #@book = Book.find_by_sql(["select * from books where UPPER(name) = UPPER(?)", params[:book_name]]).first
        @book = Book.where(["UPPER(name) = UPPER(?)", params[:book_name]]).includes(:chapters => :verses).first
        raise ActiveRecord::RecordNotFound, "Page not found" if @book.nil?
    elsif params[:id]
        @book = Book.find(params[:id].to_i, :include => {:chapters => :verses})
    end
end

Now I randomly get this error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
...
app/controllers/books_controller.rb:14:in `show'

One difference is heroku uses postgre and I'm using mysql.

Heroku url: http://kjv-on-rails.heroku.com/books/1

Full application code: https://github.com/tybro0103/KJV-on-Rails

Update I just 开发者_如何学JAVAchecked the Full Trace and realized the error occurs here:

mysql2 (0.2.6) lib/active_record/connection_adapters/mysql2_adapter.rb:635:in `select'

So the error is happening in the mysql2 gem.

Note It's been brought to my attention that I have odd/redundant relations & data. I realize this, I promise! I don't believe that's causing the error though. :)

Note By random here's what I mean... On some books I never see this error. On others I see 90% of the time, while yet on others I see it 50% of the time. I just hit refresh over and over and sometimes it works and sometimes it doesn't. I guess the only way to really see what I mean is to download the code yourself. My guess is no one will which means I'll start a bounty in a couple days as an incentive. :)

UPDATE

So the error occurs in the mysql2 adapter. Funny thing is that @book = Book.find(params[:id].to_i, :include => {:chapters => :verses}) causes the error when running rails server. In the rails console I can run the same command all day long and it works fine. It was using webrick, but I tried switching to mongrel and got the same error. I've also tried switching from ruby 1.8.7 to 1.9.2 and get the same error. It seems like maybe there is some sort of memory limit within the rails server maybe?


AHHHH... I found the trouble maker! I just downgraded mysql from 5.5 to 5.1 ...problem gone! What a pain in the @$$ that was.

It still would be nice to be able to use the latest release of mysql however. If someone can find a way to make it work using mysql 5.5 I'll mark that as the accepted answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号