# Unset relationship on the fly

**URL:** <https://www.ruby-forum.com/t/unset-relationship-on-the-fly/71913>\
**Category:** Rails\
**Created:** [October 12, 2006, 10:47am UTC](https://www.ruby-forum.com/t/unset-relationship-on-the-fly/71913 "2006-10-12T10:47:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [October 12, 2006, 10:47am UTC](https://www.ruby-forum.com/t/unset-relationship-on-the-fly/71913/1 "2006-10-12T10:47:46Z")

</div>

how can we unset an Active Record relationship of an instance on the  
fly? i have this:

class User \< AR  
has\_one :profile  
end

@user is the instance of User, and it has relationship to profile  
(@user.profile). the profile contain a large data, and it has been  
loaded into @user. the problem is, i need to save the @user into session  
without saving the profile (to avoid repetitive SQL lookup to user  
information, and to increase performance)

but the current situation is, the profile is also saved into the  
session. my session is stored in the database, so by also saving this  
profile object, it will eat my database space.

can it be done?  
thanks in advance

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [October 12, 2006, 11:41am UTC](https://www.ruby-forum.com/t/unset-relationship-on-the-fly/71913/2 "2006-10-12T11:41:26Z")

</div>

Running object.clear\_association\_cache before storing it in the session  
should do the trick.

-Jonathan.

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [October 12, 2006, 12:45pm UTC](https://www.ruby-forum.com/t/unset-relationship-on-the-fly/71913/3 "2006-10-12T12:45:32Z")

</div>

Jonathan V. wrote:

> Running object.clear\_association\_cache before storing it in the session  
> should do the trick.
> 
> -Jonathan.

thanks but it’s not working  
here’s the code where i put my clear\_association\_cache:

@session[:user] ||= {}  
user ||= @session[:user][subdomain] || User.find\_by\_login(subdomain)  
user.clear\_association\_cache  
@session[:user][subdomain] = user

and my dump result:

adrianliem: !ruby/object:User  
ad:  
attributes:  
salt: fcb964…  
updated\_at: 2006-10-12 14:12:47  
crypted\_password: 7ca5…  
id: “2”  
remember\_token:  
login: adrianliem  
created\_at: 2006-10-08 15:43:34  
email: adrianliem@localhost  
document:  
profile: !ruby/object:Profile  
attributes:  
certified:  
city: tokyo  
dob: “1980-10-09”  
location:  
message:

i’ve googled about the clear\_association\_cache, and it seems to be work  
for others, but somehow it does not work in mine. any further suggestion  
please?  
thanks in advance 🙂
