Forum: Ruby on Rails [Help] With no persistence, instances share among actions failed.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
J.C. Zhu (Guest)
on 2009-01-08 11:19
(Received via mailing list)

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

class Album < ActiveRecord::Base
        belongs_to :cd

# in action "collect"
@cd =
        Dir.foreach(Dir.pwd) do | dir |
           logger.debug "found directory : [#{dir}]"
           if dir != "." && dir != ".."
                # collect album inf
                raw_info = dir
                @al =
       = dir.to_s
       = @cd
                logger.debug "Checking album[#{}]'s belonging
                logger.debug "Find album folder [#{dir}], thus cd
contains [#{@cd.albums}]"

Processing MusicInventoryController#collect (for 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
Frederick C. (Guest)
on 2009-01-08 11:24
(Received via mailing list)
On Jan 8, 6:47 am, "J.C. Zhu" <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 ...

This topic is locked and can not be replied to.