# \[Help\] With no persistence, instances share among actions failed

**URL:** <https://www.ruby-forum.com/t/help-with-no-persistence-instances-share-among-actions-failed/157192>\
**Category:** Rails\
**Created:** [January 8, 2009, 10:19am UTC](https://www.ruby-forum.com/t/help-with-no-persistence-instances-share-among-actions-failed/157192 "2009-01-08T10:19:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![J.CSZhu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@J.CSZhu](https://www.ruby-forum.com/u/J.CSZhu)\
**Post date:** [January 8, 2009, 10:19am UTC](https://www.ruby-forum.com/t/help-with-no-persistence-instances-share-among-actions-failed/157192/1 "2009-01-08T10:19:50Z")

</div>

Hi!

My problem instance’s properties failed to share among the actions.  
e.g. In action ‘collect’, I created an “CD” instance with some  
"Album"s as its properties  
using one-many relationship provided by rails API.  
Before saving to the db, I just pass the ‘CD’ instance to action  
‘show’, using @instance\_var.  
But I can’t get properties information using ‘cd.albums’, it alway  
return an empty array.

The reason why the objects do not get persisted is that modification  
of objects is necessary.

## Why can’t I navigate through the relation and How can I pass around the instance without?

# model

class Cd \< ActiveRecord::Base  
has\_many :albums  
end

class Album \< ActiveRecord::Base  
belongs\_to :cd  
end

# in action “collect”

@cd = Cd.new  
Dir.chdir("/media/cdrom")  
Dir.foreach(Dir.pwd) do | dir |  
logger.debug “found directory : [#{dir}]”  
if dir != “.” && dir != “…”  
# collect album inf  
raw\_info = dir  
@al = Album.new  
@al.name = dir.to\_s  
@al.cd = @cd  
logger.debug “Checking album[#{@al.name}]'s belonging  
[#{@al.cd.volumn\_name}]”  
logger.debug “Find album folder [#{dir}], thus cd  
contains [#{@cd.albums}]”  
end  
end  
end

logging:  
Processing MusicInventoryController#collect (for 127.0.0.1 at  
2009-01-08 14:13:14) [GET]  
found directory : [.]  
found directory : […]  
found directory : [20090101]  
Checking album[20090101]'s belonging [test]  
Find album folder [20090101], thus cd contains []  
Rendering music\_inventory/show

---

<div class="post-metadata">

**Author:** ![J.CSZhu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@J.CSZhu](https://www.ruby-forum.com/u/J.CSZhu)\
**Post date:** [January 8, 2009, 10:24am UTC](https://www.ruby-forum.com/t/help-with-no-persistence-instances-share-among-actions-failed/157192/2 "2009-01-08T10:24:33Z")

</div>

On Jan 8, 6:47 am, “J.C. Zhu” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

You never save the @al object, so it’s not going to magically appear  
in @cd.albums. If you want to add an unsaved object to @cd.albums you  
can do  
@cd.albums.create …

Fred
