mysql 打印错误信息表,获取表不存在错误,但表确实存在(ActiveRecord :: StatementInvalid Mysql2 :: Error:表不存在)...

I am getting the following error when I try to access one of my pages

ActiveRecord::StatementInvalid (Mysql2::Error: Table 'p478r679_partybot.secretsanta' doesn't exist: SHOW FIELDS FROM 'secretsanta'):

app/controllers/secretsantas_controller.rb:7:in `index'

But this table exists in my DB.

Just to give you a background of my problem, I wrote this application few years ago and it was working fine up until recently. When I tried to start the application with thin server, I got the following error

You have already activated rack 1.4.3, but your Gemfile requires rack 1.2.1. Using bundle exec may solve this. (Gem::LoadError)

So I approached the tech support from my hosting service for assistance. They said,

“The problem was that the rails version was not compatible with the bundler version. Now we have changed the rails version to "3.1.10" and have installed the bundler "1.2.3 ". Now your website is loading fine.The "thin" service is started in your server. The port is listening to the application. Refer the details given below.”

And they have updated all my gems for get it working. I was originally using Rails 3.0.1 and thin 1.2.7. Now I have Rails 3.1.10 and thin 1.5.1. Now the application is loading, and all the features working, except Secretsanta.

When I look at the following part of the error message closely, “SHOW FIELDS FROM secretsanta” I see the table name is “secretsanta”, should it be “secretsantas”?

I am not sure what to make of this, after all the application was working fine for the past 3 years and I am not sure why it not working after updating the gems

Here is some code snip of Secretsanta model User model and SecretsantasController.

Secretsanta.rb

class Secretsanta < ActiveRecord::Base

belongs_to :user

belongs_to :gift_receiver, :class_name => "User"

...

User.rb

class User < ActiveRecord::Base

# Setup accessible (or protected) attributes for your model

attr_accessible :first_name, :last_name, :email,

:password, :password_confirmation, :remember_me

validates :first_name, :presence => true

validates :last_name, :presence => true

validate :should_be_invited

# Include default devise modules. Others available are:

# :token_authenticatable, :confirmable, :lockable and :timeoutable

devise :database_authenticatable, :registerable,

:recoverable, :rememberable, :trackable, :validatable

has_one :potluck

has_one :secretsanta

has_one :gift_receiver, :through => :secretsanta

has_many :secretsantaExclutions

has_many :excluded, :through => :secretsantaExclutions

has_many :discussions

has_many :comments

...

secretsantas_controller.rb

class SecretsantasController < ApplicationController

before_filter :authenticate_user!

def index

@exclutionList = SecretsantaExclution.find_all_by_user_id(current_user.id)

@event = Event.find_by_id(4)

@rsvp = GuestList.find_by_guest(current_user.email)

@secretsanta = Secretsanta.find_by_user_id(current_user.id) #i am getting the error at this line

end

Please let me know if you need and additional information. Thanks for your help in advance

解决方案

I believe you were right on in your suspicion about the secretsanta table name.

Unless you've set a table_name on your secretsanta model, rails will look for a table named secretsantas. If the application was working before, I'd guess that the table actually is named secretsantas.

To list the available tables, run:

tables = ActiveRecord::Base.connection.tables

Ah, here's the problem:

'Secretsanta'.pluralize

=> "Secretsanta"

Try specifying the table name in your model:

class Secretsanta < ActiveRecord::Base

self.table_name = "secretsantas"

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值