Rails 3.0.7与ruby 1.9.2 fixtures with:belongs_to和:has_many给出“表用户没有列名为posts的帖子”

时间:2011-05-28 13:42:05

标签: ruby-on-rails unit-testing fixtures

我无法加载这些灯具,我不知道为什么。花了几个小时来解决问题并开始认真质疑我对现实的把握。所以我转向这里,希望有人指出错误,希望最小的嘲笑。

我尝试过与api-docs描述相同的方式并与authlogic文档结合使用。我在SO上找到的帖子都没有任何帮助。

使用ruby 1.9.2-p180

users.yml里

one:
  id: 0
  username: testuserone
  email: whatever@whatever.com
  password_salt: <%= salt = Authlogic::Random.hex_token %>
  crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("benrocks" + salt) %>
  persistence_token: <%= Authlogic::Random.hex_token %>
  posts: one, two

two:
  id: 1
  username: testusertwo
  email: test@gmail.com
  password_salt: <%= salt = Authlogic::Random.hex_token %>
  crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("benrocks" + salt) %>
  persistence_token: <%= Authlogic::Random.hex_token %>
  posts: one, two, valid_post

posts.yml

one:
  title: First valid Awesome post
  content: Clearly awesome content
  user: one

two:
  title: Second valid Awesome post
  content: Clearly more awesome content
  user: one

valid_post:
  title: Awesome post
  content: Clearly awesome content
  user: one

empty_post:
  title: 
  content: 
  user: one 

schema.rb

ActiveRecord::Schema.define(:version => 20110527132832) do

  create_table "posts", :force => true do |t|
    t.string   "title"
    t.string   "content"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
  end

  create_table "users", :force => true do |t|
    t.string   "username"
    t.string   "email"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

post.rb - model

class Post < ActiveRecord::Base
  cattr_reader :per_page
  @@per_page = 3
  default_scope :order => 'created_at DESC' 
  validates :title, :presence => true

  belongs_to :user
end

user.rb - model

class User < ActiveRecord::Base
  attr_accessible :username, :email, :password, :password_confirmation

  acts_as_authentic

  has_many :posts
end

rake db:fixtures:load gives

耙子流产了! SQLite3 :: SQLException:表用户没有名为posts的列:INSERT INTO“users”(“id”,“username”,“email”,“password_salt”,“crypted_pa​​ssword”,“persistence_token”,“posts”,“created_at” “的updated_at”)值(0, 'testuserone', 'whatever@whatever.com', 'ce407b78abcdf7ef7a179f2ef3c4452892411fe0705fc363623f669c420204c3ceb452167a2470ef737d2d8eae6ff0eca75e1eae2f7965d2eeaf2262a3ce61df', 'ecd509731f30ed38442d4f0e6c0da81e7e747e5ded086be96a3b45b896cde0b99b314d187b1f0011ba5c6a1f661c9748fd20ac11332a413b8046ee9738167cc2', '784c46f5e1f8218592e5f46a00111a00281e53a25a36133c17de9e31f3a1e99fe8c45fc5c327b87f32dff6147b56337b900c6a1342329479dfd1c372d700a37c', '一,二',“13点28分44秒2011-05-28 ','2011-05-28 13:28:44')

正在使用的宝石

来源'http://rubygems.org'

gem'rail','3.0.7' gem'will_paginate','3.0.pre2' 宝石'authlogic' 宝石'漂亮发电机' 宝石'sqlite3' 宝石'factory_girl_rails' 宝石'faker' gem“mocha”,:group =&gt; :测试

2 个答案:

答案 0 :(得分:0)

我很长时间没有使用过灯具,但我认为你不需要在userss.yml中描述这种关联,因为你已经在posts.yml中这样做了。尝试删除这些行。

答案 1 :(得分:0)

事实证明,当我迁移测试数据库或者我给所有与用户不同的帖子名称时,它开始工作:user_one,:user_two