Rails has_many / has_one same model?

I suspect that this is what's happening: You create Collection C C is saved to the database Rails calls C. After_create You create Folder F F is saved to the database C's root_folder_id is set to F's id This change is NOT saved to the database You call F. Master_collection F queries the database, looking for Collections with coleections.

Root_folder_id = folders. Id Since C's new root_folder_id hasn't been saved, F doesn't find anything If you want to test this, call c. Save in your example code before you call f.

Master_collection and you should get the collection, like you expect (you might need a f. Reload too) That said, I've never liked double belongs_to s (Folder belongs_to Collection + Collection belongs_to root_folder). Instead, I'd recommend: class Collection 'Folder', :conditions => {:parent_id => nil} # ...other stuff... end Hope that helps!

PS: Your definition of Folder. Master_collection will only give you back a collection if you call it from a root folder - all other folders will just return nil, since no collections have root_folder_id s that point to them. Is that what you intended?

I suspect that this is what's happening: You create Collection C C is saved to the database Rails calls C. After_create You create Folder F F is saved to the database C's root_folder_id is set to F's id This change is NOT saved to the database You call F. Master_collection F queries the database, looking for Collections with coleections.

Root_folder_id = folders. Id Since C's new root_folder_id hasn't been saved, F doesn't find anything If you want to test this, call c. Save in your example code before you call f.

Master_collection - and you should get the collection, like you expect (you might need a f. Reload too). That said, I've never liked double belongs_tos (Folder belongs_to Collection + Collection belongs_to root_folder).

Instead, I'd recommend: class Collection 'Folder', :conditions => {:parent_id => nil} # ...other stuff... end Hope that helps! PS: Your definition of Folder. Master_collection will only give you back a collection if you call it from a root folder - all other folders will just return nil, since no collections have root_folder_ids that point to them.Is that what you intended?

RE: master_collection only works on Root Folder, that is what I intended. Still processing the rest of your answer, thanks! – Andrew Mar 4 at 2:09 @Andrew All's well, then - just checking.

Cheers! – Xavier Holt Mar 4 at 2:12 RE: has_one :root_folder .... :conditions { :parent_id => nil }. There can be more than one collection and each one has a root folder.

How does ActiveRecord know which folder with nil parent_id belongs to which collection? – Andrew Mar 4 at 2:24 1 @Andrew - There's an implicit :foreign_key => :collection_id in that statement. It's a Rails convention thing.

That same implicit foreign key lets you just call has_many :folders - Rails will assume that anything that belongs to Model MyModel will do it with my_model_id (unless you tell it otherwise). – Xavier Holt Mar 4 at 2:31 OH! So- using the existing collection_id, where collection_id = X AND parent_id = nil == root_folder.

Nice! Yes, I like that a lot more than the double belongs_to approach. – Andrew Mar 4 at 2:33.

The error is that the CamelCase model name is converted to underscorey (sorry, I can't remember the term for that right now) case. All your references to companyrelationship should add an underscore. You can override the convention by setting the :class_name attribute, but I don't think that's what you wanted.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions