Combining Two Models in Rails for a Form

Posted by matsko on Stack Overflow See other posts from Stack Overflow or by matsko
Published on 2010-06-01T02:56:36Z Indexed on 2010/06/01 3:03 UTC
Read the original article Hit count: 294

Filed under:
|
|
|
|

Hey Guys.

I'm very new with rails and I've been building a CMS application backend.

All is going well, but I would like to know if this is possible?

Basically I have two models:

@page { id, name, number }

@extended_page { id, page_id, description, image }

The idea is that there are bunch of pages but NOT ALL pages have extended_content. In the event that there is a page with extended content then I want to be able to have a form that allows for editing both of them.

In the controller:

@page = Page.find(params[:id]) @extended= Extended.find(:first, :conditions => ["page_id = ?",@page.id]) @combined = ... #merge the two somehow

So in the view:

<%- form_for @combined do |f| %>

<%= f.label :name %> <%= f.text_field :name %>

...

<%= f.label :description %> <%= f.text_field :description %>

<%- end >

This way in the controller, there only has to be one model that will be updated (which will update to both).

Is this possible?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby