Rails 3.0.7 与 ruby​​ 1.9.2 固定装置具有 :belongs_to 和 :has_many 给出“表用户没有名为 posts 的列”

发布于 2024-11-10 22:50:05 字数 3685 浏览 3 评论 0原文

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

我尝试按照 api-docs 描述的方式进行操作,并与 < href="http://rdoc.info/github/binarylogic/authlogic/master/Authlogic/TestCase" rel="nofollow">authlogic 文档。我在这里找到的所有帖子都没有任何帮助。

使用 ruby​​ 1.9.2-p180

users.yml

one:
  id: 0
  username: testuserone
  email: [email protected]
  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: [email protected]
  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") VALUES (0, 'testuserone', '[电子邮件受保护]', 'ce407b78abcdf7ef7a179f2ef3c4452892411fe0705fc363623f669c420204c3ceb452167a2470ef737d2d8eae6ff0eca75e1eae2f7965d2eeaf2262a3ce61df', 'ecd509731f30ed38442d4f0e6c0da81e7e747e5ded086be96a3b45b896cde0b99b314d187b1f0011ba5c6a1f661c9748fd20ac11332a413b8046ee9738167cc2', '784c46f5e1f8218592e5f46a00111a00281e53a25a36133c17de9e31f3a1e99fe8c45fc5c327b87f32dff6147b56337b900c6a1342329479dfd1c372d700a37c', 'one, two', '2011-05-28 13:28:44', '2011-05-28 13:28:44')

Gems in使用

源 'http://rubygems.org'

gem 'rails', '3.0.7' gem 'will_paginate', '3.0.pre2' 宝石“真实逻辑” gem '漂亮的发电机' 宝石'sqlite3' 宝石'factory_girl_rails' 宝石“假冒者” 宝石“摩卡”,:组=> :测试

I can't get these fixtures to load and I don't know why. After spending a few hours on the problem and starting to seriously question my grasp of reality. So I turn here in hope of someone pointing out the error with hopefully minimal ridicule.

I have tried doing it the same way as the api-docs describe and combining with the authlogic documentation. None of the posts I could find here on SO were of any help either.

Using ruby 1.9.2-p180

users.yml

one:
  id: 0
  username: testuserone
  email: [email protected]
  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: [email protected]
  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

rake aborted!
SQLite3::SQLException: table users has no column named posts: INSERT INTO "users" ("id", "username", "email", "password_salt", "crypted_password", "persistence_token", "posts", "created_at", "updated_at") VALUES (0, 'testuserone', '[email protected]', 'ce407b78abcdf7ef7a179f2ef3c4452892411fe0705fc363623f669c420204c3ceb452167a2470ef737d2d8eae6ff0eca75e1eae2f7965d2eeaf2262a3ce61df', 'ecd509731f30ed38442d4f0e6c0da81e7e747e5ded086be96a3b45b896cde0b99b314d187b1f0011ba5c6a1f661c9748fd20ac11332a413b8046ee9738167cc2', '784c46f5e1f8218592e5f46a00111a00281e53a25a36133c17de9e31f3a1e99fe8c45fc5c327b87f32dff6147b56337b900c6a1342329479dfd1c372d700a37c', 'one, two', '2011-05-28 13:28:44', '2011-05-28 13:28:44')

Gems in use

source 'http://rubygems.org'

gem 'rails', '3.0.7'
gem 'will_paginate', '3.0.pre2'
gem 'authlogic'
gem 'nifty-generators'
gem 'sqlite3'
gem 'factory_girl_rails'
gem 'faker'
gem "mocha", :group => :test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-11-17 22:50:05

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

I haven't used fixtures for a long time, but I don't think you need to describe the association in users.yml since you already do that in posts.yml. Try removing those lines.

子栖 2024-11-17 22:50:05

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

Well turns out it started working either when I migrated the test db or when I gave all posts names that differed from users like :user_one, :user_two

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文