On Feb 22, 2010, at 4:22 PM, Alex Bowyer wrote:

Hi, I have a data model like this:

event
has_many :topics, :accessible => true
has_many :themes, :accessible => true
has_many :sessions

basically, if an event has 2 themes and 2 topics, it will have four
sessions:
(topicA+theme1, topicB+theme2, topicA+theme2, topicB+theme1)

I am modifying the "new" action and I want to add some code to create
these session objects after the user saves their new event.

I assume I want to do something like this:

def new
  hobo_new
  # eventid = id of newly created event (how do I do this)
  themes.each do |theme|
     topics.each do |topic|
         session = Session.new
         session.event_id=eventid
         session.topic_id=topic.id
         session.theme_id=theme.id
     end
 end
end

What I can't figure out is how to get the id of the newly created
event so that I can use it when creating the child session objects.

Inside the hobo_new block, you could use the standard 'this':

def new
  hobo_new do
    if this.errors.empty?
      event_id = this.id
      themes.each do |theme|
        topics.each do |topic|
          session = Session.new
          session.event_id=event_id
          session.topic_id=topic.id
          session.theme_id=theme.id
        end
      end
    end
  end
end

But that's quite a mess to do in a controller, so I tend to agree with kevinpfromnm that this belongs in a model callback.

I'm also curious as to how much functionality is actually attached to the Topic and Theme models - are they just basic [id,name] tuples? You might be better off not even creating models for them, and just setting up some virtual attributes that handle setting up the sessions.

--Matt Jones

--
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en.

Reply via email to