good night. have problems first rails app. have 2 models user , profile. have association 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
there factories:
factories.rb
factorygirl.define factory :user username "valik leontiev" email "leontiev@example.com" password "foobaryea" password_confirmation "foobaryea" end factory :profile city "vladikavkaz" gamelevel "m1" phone "8029383744" aboutme "hello, friend!" user #association user end end
and i've tested model rspec:
user_spec.rb require 'spec_helper'
describe user do
before @user = user.new(username: "example user", email: "user@example.com", password: "foobaryea", password_confirmation: "foobaryea") end subject { @user } describe "profiles associations" before { @user.save } let(:profile) factorygirl.create(:profile, user: @user) end "should destroy associated profile" @profile = @user.profile @user.destroy @profile.should_not be_empty profile.find_by_id(profile.id).should be_nil end "should have not more 1 profile" profile.where(user_id: @user.id).count > 2 should_not true end end end
and had 1 failure in "should destroy associated profile" blocks: nomethoderror: undefined method `empty?'. so, how undersanding, @user.profile nil, why association has not building? i've add "user" in profile factories associate. me, please
nothing wrong rails code , @user.profile
may valid. activerecord
class not have empty?
method, is_empty
matcher relies on. should using other matcher if want test @profile
object still valid after associated user destroyed.
here's output console session shows creation of user
, creation of associated profile
, destroying of user
, impact of in-memory , saved profile
.
2.0.0p247 :001 > user = user.create (0.2ms) begin binds [#<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 "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 [#<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 "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 nil profile load (1.9ms) select "profiles".* "profiles" "profiles"."user_id" = $1 order "profiles"."id" asc limit 1 [["user_id", 6]] binds nil sql (0.8ms) delete "profiles" "profiles"."id" = $1 [["id", 4]] binds nil sql (0.5ms) delete "users" "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".* "profiles" "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 >
Comments
Post a Comment