晚安。我的第一个rails应用程序遇到了一些问题。 我有两个模型User和Profile。它有关联has_one和belongs_to
User.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me,:username
validates :username, presence: true
has_one :profile, dependent: :destroy
end
Profile.rb
class Profile < ActiveRecord::Base
attr_accessible :aboutme, :city, :gamelevel, :phone
belongs_to :user
validates :user_id, presence: true
validates :phone, length: { in: 3..12 }
validates :phone, numericality: { only_integer: true }
end
我的工厂有:
factories.rb
FactoryGirl.define do
factory :user do
username "Valik Leontiev"
email "Leontiev@example.com"
password "foobaryea"
password_confirmation "foobaryea"
end
factory :profile do
city "Vladikavkaz"
gamelevel "M1"
phone "8029383744"
aboutme "Hello, my friend!"
user #association with user
end
end
我用rspec测试了我的模型:
user_spec.rb 需要'spec_helper'
描述用户执行
before do
@user = User.new(username: "Example User", email: "user@example.com",
password: "foobaryea", password_confirmation: "foobaryea")
end
subject { @user }
describe "profiles associations" do
before { @user.save }
let(:profile) do
FactoryGirl.create(:profile, user: @user)
end
it "should destroy associated profile" do
@profile = @user.profile
@user.destroy
@profile.should_not be_empty
Profile.find_by_id(profile.id).should be_nil
end
it "should have not more then one profile" do
Profile.where(user_id: @user.id).count > 2
should_not be true
end
end
end
我在“应该销毁关联的配置文件”块中遇到一个失败:NoMethodError:未定义的方法`empty?'。所以,我是如何疏忽,@ user.profile是零,为什么协会没有建立?我在个人资料工厂中添加“用户”。 请帮帮我
答案 0 :(得分:0)
您的Rails代码没有任何问题,@user.profile
可能完全有效。 ActiveRecord
类没有empty?
方法,这是is_empty
匹配器所依赖的方法。如果要在关联用户被销毁后测试@profile
对象仍然有效,则应使用其他匹配器。
这是控制台会话的输出,显示User
的创建,相关Profile
的创建,User
的破坏以及内存中的影响和已保存Profile
。
2.0.0p247 :001 > user = User.create
(0.2ms) BEGIN
Binds are [#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007ff0b24c1d50 @oid_type=#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Timestamp:0x007ff0b2962440>, @array=false, @name="updated_at", @sql_type="timestamp without time zone", @null=true, @limit=nil, @precision=nil, @scale=nil, @type=:datetime, @default=nil, @primary=false, @coder=nil>, Fri, 12 Jul 2013 14:12:19 UTC +00:00]
SQL (10.3ms) INSERT INTO "users" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", Fri, 12 Jul 2013 14:12:19 UTC +00:00], ["updated_at", Fri, 12 Jul 2013 14:12:19 UTC +00:00]]
(0.7ms) COMMIT
=> #<User id: 6, created_at: "2013-07-12 14:12:19", updated_at: "2013-07-12 14:12:19">
2.0.0p247 :002 > profile = Profile.create(user_id: user.id)
(0.3ms) BEGIN
Binds are [#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007ff0b41d3060 @oid_type=#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Timestamp:0x007ff0b2962440>, @array=false, @name="updated_at", @sql_type="timestamp without time zone", @null=true, @limit=nil, @precision=nil, @scale=nil, @type=:datetime, @default=nil, @primary=false, @coder=nil>, Fri, 12 Jul 2013 14:12:39 UTC +00:00]
SQL (3.3ms) INSERT INTO "profiles" ("created_at", "updated_at", "user_id") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Fri, 12 Jul 2013 14:12:39 UTC +00:00], ["updated_at", Fri, 12 Jul 2013 14:12:39 UTC +00:00], ["user_id", 6]]
(0.4ms) COMMIT
=> #<Profile id: 4, created_at: "2013-07-12 14:12:39", updated_at: "2013-07-12 14:12:39", user_id: 6>
2.0.0p247 :003 > user.destroy
(0.4ms) BEGIN
Binds are nil
Profile Load (1.9ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1 [["user_id", 6]]
Binds are nil
SQL (0.8ms) DELETE FROM "profiles" WHERE "profiles"."id" = $1 [["id", 4]]
Binds are nil
SQL (0.5ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 6]]
(0.4ms) COMMIT
=> #<User id: 6, created_at: "2013-07-12 14:12:19", updated_at: "2013-07-12 14:12:19">
2.0.0p247 :004 > Profile.find_by_id(4)
Profile Load (0.7ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = 4 LIMIT 1
=> nil
2.0.0p247 :005 > profile
=> #<Profile id: 4, created_at: "2013-07-12 14:12:39", updated_at: "2013-07-12 14:12:39", user_id: 6>
2.0.0p247 :006 >