# Combining two models in one controller/view "set"

**URL:** <https://www.ruby-forum.com/t/combining-two-models-in-one-controller-view-set/56188>\
**Category:** Rails\
**Created:** [March 28, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/combining-two-models-in-one-controller-view-set/56188 "2006-03-28T20:42:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gravyface](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@gravyface](https://www.ruby-forum.com/u/gravyface)\
**Post date:** [March 28, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/combining-two-models-in-one-controller-view-set/56188/1 "2006-03-28T20:42:29Z")

</div>

I’m going to take another stab at this question (my first one received  
no replies):

I have two models:

Author  
has\_many: books

Book  
has\_one: author

I have two tables:

authors  
id  
…

books  
id  
author\_id  
…

I let the scaffolding fly and it created the CRUD goodness for each  
model, as expected.

However, I want to have only one view/controller “set” for both  
models; when I refer to “set”, I mean having a generic controller  
called Library with methods for create, edit, show, destroy, and list,  
but these methods would access and manipulate this combined data set  
of authors and their books from both models.

After sifting through both the controllers and views and adding  
those relationships (described above) is there any way to:

a) run the scaffolding again so that it will take these new  
relationships into account?  
b) if not, how should I proceed?

I am a complete Ruby/RoR/MVC luddite and am open to any suggestions  
here.

Thanks

---

<div class="post-metadata">

**Author:** ![gravyface](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@gravyface](https://www.ruby-forum.com/u/gravyface)\
**Post date:** [March 28, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/combining-two-models-in-one-controller-view-set/56188/2 "2006-03-28T22:13:57Z")

</div>

gravy face wrote:

> I’m going to take another stab at this question (my first one received  
> no replies):
> 
> I have two models:
> 
> Author  
> has\_many: books
> 
> Book  
> has\_one: author
> 
> I have two tables:
> 
> authors  
> id  
> …
> 
> books  
> id  
> author\_id  
> …
> 
> I let the scaffolding fly and it created the CRUD goodness for each  
> model, as expected.
> 
> However, I want to have only one view/controller “set” for both  
> models; when I refer to “set”, I mean having a generic controller  
> called Library with methods for create, edit, show, destroy, and list,  
> but these methods would access and manipulate this combined data set  
> of authors and their books from both models.
> 
> After sifting through both the controllers and views and adding  
> those relationships (described above) is there any way to:
> 
> a) run the scaffolding again so that it will take these new  
> relationships into account?  
> b) if not, how should I proceed?
> 
> I am a complete Ruby/RoR/MVC luddite and am open to any suggestions  
> here.
> 
> Thanks  
> Scaffolding[1]:  
> Scaffolding is a temporary framework used to support people and material  
> in the construction or repair of buildings and other large structures.

You cannot do this with scaffolding, you need to write this yourself. I  
suggest you buy or read Agile Web D. with rails.

Joey\_\_  
[1] [Scaffolding - Wikipedia](http://en.wikipedia.org/wiki/Scaffolding)

---

<div class="post-metadata">

**Author:** ![gravyface](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@gravyface](https://www.ruby-forum.com/u/gravyface)\
**Post date:** [March 28, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/combining-two-models-in-one-controller-view-set/56188/3 "2006-03-28T23:38:15Z")

</div>

As for the book, I have it, I haven’t had a chance to read it yet  
(only skimmed through it). And as for the answer, I was hoping this  
was a common “pattern” that somebody has stumbled across before and  
perhaps blogged or wiki’ed it; Google came up with nothing although my  
terms are pretty ambiguous.

+1 for the burn – but at least I got a response this time. 🙂

---

<div class="post-metadata">

**Author:** ![gravyface](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@gravyface](https://www.ruby-forum.com/u/gravyface)\
**Post date:** [March 29, 2006, 1:24am UTC](https://www.ruby-forum.com/t/combining-two-models-in-one-controller-view-set/56188/4 "2006-03-29T01:24:39Z")

</div>

On 3/28/06, GravyFace [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> After sifting through both the controllers and views and adding  
> those relationships (described above) is there any way to:
> 
> a) run the scaffolding again so that it will take these new  
> relationships into account?  
> b) if not, how should I proceed?
> 
> I am a complete Ruby/RoR/MVC luddite and am open to any suggestions here.

It might not be completely what you want, but you could look into the  
Scaffolding Extensions Plugin:  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/Scaffolding+Extensions+Plugin).

---

<div class="post-metadata">

**Author:** ![gravyface](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@gravyface](https://www.ruby-forum.com/u/gravyface)\
**Post date:** [March 29, 2006, 1:36am UTC](https://www.ruby-forum.com/t/combining-two-models-in-one-controller-view-set/56188/5 "2006-03-29T01:36:13Z")

</div>

Perfect, thanks man. I’m not planning on leaning on the scaffolding  
crutch forever, but c’mon, who didn’t switch to Rails because of it?  
🙂

---

<div class="post-metadata">

**Author:** ![gravyface](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/eb8c5e/32.png) [@gravyface](https://www.ruby-forum.com/u/gravyface)\
**Post date:** [March 29, 2006, 4:08am UTC](https://www.ruby-forum.com/t/combining-two-models-in-one-controller-view-set/56188/6 "2006-03-29T04:08:24Z")

</div>

The sooner you ditch scaffold the sooner you will grok rails. I swear  
that feature has done as much harm as good.

On 3/28/06, GravyFace [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > relationships into account?  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Jeremy H.

> **[Jeremy Huffman, the Website - jeremyhuffman.com](https://jeremyhuffman.com/)**
>
> Jeremy Huffman, the Website
>         
>       
>     
>     
> Getting in Touch
> 
> You can email me if you can guess the addres...
