0

晚安。我的第一个 Rails 应用程序有一些问题。我有两个模型用户和个人资料。它有关联 has_one 和 belongs_to

用户.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

简介.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

有我的工厂:

工厂.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: undefined method `empty?'。那么,我是如何理解的,@user.profile 为零,为什么关联没有建立?我已经在配置文件工厂中为关联添加了“用户”。请帮帮我

4

1 回答 1

0

您的 Rails 代码没有任何问题,并且@user.profile可能完全有效。该类ActiveRecord没有empty?方法,这是is_empty匹配器所依​​赖的。@profile如果您想在关联用户被销毁后测试该对象是否仍然有效,您应该使用其他匹配器。

这是来自控制台会话的输出,显示了 aUser的创建、关联的创建、内存中和已保存的Profile的破坏以及影响。UserProfile

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 > 
于 2013-07-12T00:36:42.260 回答