def configure @project = Project.find( params[:id], :include => { :floors => :rooms } ) @floors = @project.floors @rooms = @froors.map { |f| f.rooms }.flatten end
- Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Sat, Jun 13, 2009 at 10:18 AM, jhaagmans<jaap.haagm...@gmail.com> wrote: > > Hi, > > I'm working on a large project that handles quite some AJAX and > database interactions in short periods of time. > > I'll simplify the project to an example. Say we're building houses and > a housebuilding project consists of floors and rooms, then to load a > configurator to edit the room and floor structure we would want to > load a project, which contains a few floors and each floor will > contain a few rooms. I would want to load the floors in a project, but > also the rooms in the floor that has been set to active using the "a" > flag. The other rooms are loaded when another floor is activated. > > The models are built in a way so that a project has_many floors and a > floor has_many rooms. A room belongs_to a floor and a floor belongs_to > a project. > > I could do this: > > def configure > �...@project = Project.find(:first, :conditions => { :id => params > [:id] } ) > �...@floors = Floor.find(:all, :conditions => { :project_id => > @project.id }) > �...@rooms = Room.find(:all, :conditions => { :floor_id => Floor.find > (:first, :conditions => { :project_id => @project.id, :a => > true } ).id }) > end > > But here I'm executing the Floor.find method twice and I feel that's > not very DNY. Now I can think of two things to do. The first one is to > get the line out of @floors where "a" is true, but I can only think of > a for loop to do that and I feel there must be a simpler way. Is > there? > > The second way is a way in which I would use more of the model's > features. So load the project and all its children (floors and rooms). > But I cannot find any documentation on that. I want this done the > Rails way and I really have a feeling that I could easily cut the > number of database requests. Either way would be fine. > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---