问题
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: 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
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', 'whatever@whatever.com', '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
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/6161988/rails-3-0-7-with-ruby-1-9-2-fixtures-with-belongs-to-and-has-many-gives-table