我命名了一个表Types
,并意识到它是一个保留字,所以我将其重命名为'Category`。
现在,当我尝试执行ActiveRecord查询时,出现此错误:
PG::UndefinedTable: ERROR: relation "categories" does not exist
LINE 5: WHERE a.attrelid = '"categories"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"categories"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
我已经回过去了所有的代码,并改变了所有的关系名称,模型,控制器等......但我不知道这个错误来自哪里。或者它意味着什么。这是我的category.rb:
class Category < ActiveRecord::Base
has_many :projects
end
这里是categories_controller.rb: class CategoriesController&lt; ApplicationController中
def create
@category = Category.new(category_params)
if @category.save
redirect_to root_path
else
redirect_to({:controller => 'application', :action => 'home'}, :notice => 'Account not created! Something is fishy.')
end
end
end
以下是我在用户模型中尝试做的事情(因为我希望代码沿着current_user.contributions_by_category
def contributions_by_category(array)
category = {}
array.each do |subarray|
name = Category.find(subarray[1].category_id).project_category;
category[name] = 0 unless category.include?(name)
category[name] += subarray[0] unless subarray[0] == nil
end
category
end
另外,在我的项目模型中,我有:
class Project < ActiveRecord::Base
belongs_to :user, :foreign_key => 'user_id'
belongs_to :category, :foreign_key => 'category_id' #should change this to a has one association
has_many :users
has_many :payments
has_many :updates
has_many :comments
has_one :imapct
....
end
在我切换名称之前,我能够使这个查询在rails控制台中工作并将其渲染到屏幕上,因此关系存在,我所做的就是更改名称。
感谢任何帮助!
更新:
这是来自rails console的表数据:
List of relations
Schema | Name | Type | Owner
--------+-------------------+-------+-------------
public | categories | table | seinnaidess
public | comments | table | seinnaidess
public | entrepreneurs | table | seinnaidess
public | impacts | table | seinnaidess
public | payments | table | seinnaidess
public | projects | table | seinnaidess
public | schema_migrations | table | seinnaidess
public | update_comments | table | seinnaidess
public | updates | table | seinnaidess
public | users | table | seinnaidess
这是我的架构:
ActiveRecord::Schema.define(version: 20150930040619) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "categories", force: :cascade do |t|
t.string "category_type"
end
create_table "comments", force: :cascade do |t|
t.text "content"
t.integer "user_id"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "entrepreneurs", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "password_digest"
t.text "about"
t.string "location"
t.integer "age"
t.float "repayment_rate"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "impacts", force: :cascade do |t|
t.integer "ppl_hired", default: 0
t.integer "ppl_schooled", default: 0
t.integer "improved_housing", default: 0
t.integer "improved_nourishment", default: 0
t.integer "improved_medical", default: 0
t.integer "community_resource", default: 0
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "payments", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.float "amount"
t.integer "cc_num"
t.integer "exp_month"
t.integer "exp_year"
t.integer "cvs"
t.integer "user_id"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
t.text "comments"
end
create_table "projects", force: :cascade do |t|
t.string "name"
t.text "url"
t.text "description"
t.float "money_needed", default: 0.0
t.float "money_raised", default: 0.0
t.float "interest_offered", default: 0.0
t.datetime "end_date"
t.integer "category_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "update_comments", force: :cascade do |t|
end
create_table "updates", force: :cascade do |t|
t.text "content"
t.string "url"
t.integer "project_id"
t.string "date"
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "user_name"
t.string "password_digest"
t.string "impact"
t.text "pic_url"
t.text "quote"
t.integer "payment_id"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "admin"
end