Unset relationship on the fly

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

Running object.clear_association_cache before storing it in the session
should do the trick.

-Jonathan.

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: [email protected]
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 :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs