我可能错过了一些微不足道的事情......
我有一个用户模型' has_one'用户配置:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_create :create_remember_token
has_many :user_roles, dependent: :destroy
has_many :roles, through: :user_roles
has_one :user_profile, dependent: :destroy
accepts_nested_attributes_for :user_roles, :user_profile, allow_destroy: true
validates :email, presence: true, format: {with: TextUtils::VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
has_secure_password
validates :password, length: { minimum: 6 }
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
def admin?
in_role?(Role::ADMINISTRATOR)
end
def in_role?(role)
!self.roles.find_by_name(role).nil?
end
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
end
我有一个UserProfile模型&#39; belongs_to&#39;用户:
class UserProfile < ActiveRecord::Base
belongs_to :user
has_one :address, dependent: :destroy
accepts_nested_attributes_for :address
validates :first, :last, :ssn, :title, :division, :phone_home, :phone_mobile, :phone_work, :phone_work_ext,
:emergency_phone, :emergency_name, :emergency_relation, presence: true
validates :personal_email, format: { with: TextUtils::VALID_EMAIL_REGEX }
end
当我尝试为这样的数据库播种时(或者将配置文件分配给用户的任何其他方式):
# Admin
admin = User.create!(email: "admin@work.com", password: "secret", password_confirmation: "secret",
user_profile: UserProfile.new(
first: "Admin",
last: "User",
address_attributes: {line1: "123 Fake Street", line2: "Suite 1", city: "Some City", state: "FL", zip_code: "11111"},
ssn: "123-45-6789",
phone_work: "(111) 222-3333",
phone_work_ext: "110",
personal_email: "test@home.com")
)
我一直收到以下错误:
rake aborted! ActiveRecord::UnknownAttributeError: unknown attribute: user_profile_id C:/work/Portal/db/seeds.rb:12:in `' Tasks: TOP => db:seed (See full trace by running task with --trace)
我不知道为什么会发生这种情况。任何帮助将不胜感激!我正在使用Rails 4.1.4,如果这很重要。
答案 0 :(得分:0)
所有内容都必须作为(嵌套)哈希传递:
admin = User.create!({
email: "admin@work.com",
password: "secret",
password_confirmation: "secret",
user_profile_attributes: {
first: "Admin",
last: "User",
address_attributes: {
line1: "123 Fake Street",
line2: "Suite 1",
city: "Some City",
state: "FL",
zip_code: "11111"
},
ssn: "123-45-6789",
phone_work: "(111) 222-3333",
phone_work_ext: "110",
personal_email: "test@home.com"
}
})
工作示例:
$ rails new testapplication
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/views/layouts/application.html.erb
create app/assets/images/.keep
create app/mailers/.keep
create app/models/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/secrets.yml
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/backtrace_silencers.rb
create config/initializers/cookies_serializer.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/session_store.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/robots.txt
create test/fixtures
create test/fixtures/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.keep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.keep
run bundle install
Your user account isn't allowed to install to the system Rubygems.
You can cancel this installation and run:
bundle install --path vendor/bundle
to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to Rubygems using sudo.
$ cd testapplication/
$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Installing rake 10.3.2
Installing i18n 0.6.11
Using json 1.8.1
Installing minitest 5.4.1
Installing thread_safe 0.3.4
Installing tzinfo 1.2.2
Installing activesupport 4.1.1
Installing builder 3.2.2
Installing erubis 2.7.0
Installing actionview 4.1.1
Installing rack 1.5.2
Installing rack-test 0.6.2
Installing actionpack 4.1.1
Installing mime-types 1.25.1
Installing polyglot 0.3.5
Installing treetop 1.4.15
Installing mail 2.5.4
Installing actionmailer 4.1.1
Installing activemodel 4.1.1
Installing arel 5.0.1.20140414130214
Installing activerecord 4.1.1
Using bundler 1.6.1
Installing coffee-script-source 1.8.0
Installing execjs 2.2.1
Installing coffee-script 2.3.0
Installing thor 0.19.1
Installing railties 4.1.1
Installing coffee-rails 4.0.1
Installing hike 1.2.3
Installing multi_json 1.10.1
Installing jbuilder 2.1.3
Installing jquery-rails 3.1.1
Installing tilt 1.4.1
Installing sprockets 2.11.0
Installing sprockets-rails 2.1.3
Installing rails 4.1.1
Installing rdoc 4.1.1
Installing sass 3.2.19
Installing sass-rails 4.0.3
Installing sdoc 0.4.1
Installing spring 1.1.3
Installing sqlite3 1.3.9
Installing turbolinks 2.3.0
Installing uglifier 2.5.3
Your bundle is complete!
It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
$ ./bin/rails g model user
invoke active_record
create db/migrate/20140829201904_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
$ ./bin/rails g model user_profile user:references test:integer
invoke active_record
create db/migrate/20140829201916_create_user_profiles.rb
create app/models/user_profile.rb
invoke test_unit
create test/models/user_profile_test.rb
create test/fixtures/user_profiles.yml
$ ./bin/rake db:migrate
== 20140829201904 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0012s
== 20140829201904 CreateUsers: migrated (0.0013s) =============================
== 20140829201916 CreateUserProfiles: migrating ===============================
-- create_table(:user_profiles)
-> 0.0010s
== 20140829201916 CreateUserProfiles: migrated (0.0010s) ======================
然后编辑了用户模型:
class User < ActiveRecord::Base
has_one :user_profile
accepts_nested_attributes_for :user_profile
end
UserProfile已经是正确的:
class UserProfile < ActiveRecord::Base
belongs_to :user
end
并运行create statement:
$ ./bin/rails c
Loading development environment (Rails 4.1.1)
2.1.0 :001 > User.create({user_profile_attributes: {test: 5}})
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-08-29 20:21:41.860036"], ["updated_at", "2014-08-29 20:21:41.860036"]]
SQL (0.4ms) INSERT INTO "user_profiles" ("created_at", "test", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["created_at", "2014-08-29 20:21:41.864624"], ["test", 5], ["updated_at", "2014-08-29 20:21:41.864624"], ["user_id", 1]]
(1.3ms) commit transaction
=> #<User id: 1, created_at: "2014-08-29 20:21:41", updated_at: "2014-08-29 20:21:41">
2.1.0 :002 > User.last.user_profile.inspect
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
UserProfile Load (0.3ms) SELECT "user_profiles".* FROM "user_profiles" WHERE "user_profiles"."user_id" = ? LIMIT 1 [["user_id", 1]]
=> "#<UserProfile id: 1, user_id: 1, test: 5, created_at: \"2014-08-29 20:21:41\", updated_at: \"2014-08-29 20:21:41\">"