【问题标题】:Rails 3.0.7 with ruby 1.9.2 fixtures with :belongs_to and :has_many gives "table users has no column named posts"带有 :belongs_to 和 :has_many 的 ruby​​ 1.9.2 固定装置的 Rails 3.0.7 给出“表用户没有名为帖子的列”
【发布时间】:2011-09-03 22:55:47
【问题描述】:

我无法加载这些装置,我也不知道为什么。在这个问题上花了几个小时并开始认真质疑我对现实的把握之后。所以我转过来希望有人指出错误,希望能尽量减少嘲笑。

我已尝试以与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 - 模型

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 - 模型

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

  acts_as_authentic

  has_many :posts
end

rake db:fixtures:load 给出

rake 中止! 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 'rails', '3.0.7' 宝石'will_paginate','3.0.pre2' 宝石'authlogic' 宝石'漂亮的发电机' 宝石'sqlite3' 宝石'factory_girl_rails' 宝石'假货' 宝石“摩卡”,:组=>:测试

【问题讨论】:

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


    【解决方案1】:

    我已经很久没有使用固定装置了,但我认为您不需要在 users.yml 中描述关联,因为您已经在 posts.yml 中这样做了。尝试删除这些行。

    【讨论】:

    • 固定装置已加载,但帖子和用户之间似乎没有任何关联。 Post.first => # User.first => # []
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      相关资源
      最近更新 更多