# Problem with creating a database scheme/model

**URL:** <https://www.ruby-forum.com/t/problem-with-creating-a-database-scheme-model/126864>\
**Category:** Rails\
**Created:** [January 22, 2008, 10:14pm UTC](https://www.ruby-forum.com/t/problem-with-creating-a-database-scheme-model/126864 "2008-01-22T22:14:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris-Aix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@Chris-Aix](https://www.ruby-forum.com/u/Chris-Aix)\
**Post date:** [January 22, 2008, 10:14pm UTC](https://www.ruby-forum.com/t/problem-with-creating-a-database-scheme-model/126864/1 "2008-01-22T22:14:05Z")

</div>

Hi there,

I have a problem with creating a database scheme and the according  
models.  
To describe the situation I made small scribble, because I think that  
explains it best.

[http://herrbuerger.com/scheme.001.jpg](http://herrbuerger.com/scheme.001.jpg)

So I have several pages and each page has many texts, pictures and  
assets. So far the models look like this:

Page  
has\_many :texts  
has\_many :pictures  
has\_many :assets

Texts  
belongs\_to :page

Picture  
belongs\_to :page

Asset  
belongs\_to :page

Right now i have to query each table (pictures, texts, assets)  
seperately to get all the stuff that belongs to ONE page.

What I want to do is just fire one query to get all the items. I  
really rack my brain on this one.  
Hopefully one of you guys can give me a hint on how to do this.

Thanks for your time,  
Chris

(note: I posted this to the other Rails Mailinglist, because I did not  
know about this one. However this list seems to be the more active  
one.)

---

<div class="post-metadata">

**Author:** ![Chris-Aix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@Chris-Aix](https://www.ruby-forum.com/u/Chris-Aix)\
**Post date:** [January 22, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/problem-with-creating-a-database-scheme-model/126864/2 "2008-01-22T22:22:49Z")

</div>

If I understand you correctly: Personally I’d do a find\_by\_sql with a  
couple of joins in but I’m told you can use the acts\_as\_ferret plugin  
too

---

<div class="post-metadata">

**Author:** ![Chris-Aix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@Chris-Aix](https://www.ruby-forum.com/u/Chris-Aix)\
**Post date:** [January 22, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/problem-with-creating-a-database-scheme-model/126864/3 "2008-01-22T22:54:30Z")

</div>

Chris-Aix wrote:

> Page  
> has\_many :texts  
> has\_many :pictures  
> has\_many :assets
> 
> Texts  
> belongs\_to :page
> 
> Picture  
> belongs\_to :page
> 
> Asset  
> belongs\_to :page
> 
> Right now i have to query each table (pictures, texts, assets)  
> seperately to get all the stuff that belongs to ONE page.
> 
> What I want to do is just fire one query to get all the items. I  
> really rack my brain on this one.  
> Hopefully one of you guys can give me a hint on how to do this.

p = Page.find :first  
p.texts, p.pictures, p.assets

If you mean that rails has to make all the queries then search on eager  
loading in the rails api

---

<div class="post-metadata">

**Author:** ![Chris-Aix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@Chris-Aix](https://www.ruby-forum.com/u/Chris-Aix)\
**Post date:** [January 23, 2008, 11:48am UTC](https://www.ruby-forum.com/t/problem-with-creating-a-database-scheme-model/126864/4 "2008-01-23T11:48:52Z")

</div>

ok. i read the stuff about eager loading and it helped me out a little  
bit.

right now i am doing something like this:  
page = Page.find(:first, :include =\> [:pictures,:assets, :texts])

so this query gets all the results i need. the problem is each of this  
tables has a position field and the endresult should be ordered this  
way.  
i made another scribble to explain it better.

[http://herrbuerger.com/scribble\_list.001.jpg](http://herrbuerger.com/scribble_list.001.jpg)

any ideas?

---

<div class="post-metadata">

**Author:** ![Chris-Aix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@Chris-Aix](https://www.ruby-forum.com/u/Chris-Aix)\
**Post date:** [January 23, 2008, 9:04am UTC](https://www.ruby-forum.com/t/problem-with-creating-a-database-scheme-model/126864/5 "2008-01-23T09:04:08Z")

</div>

Thanks for your tips.

The first thing that came to my mind was find\_by\_sql, but I guess that  
the queries are not that easy.  
And, Ilan, the first solution that you recommended is like the one I  
use now.

And to specify my problem a little bit more I’ll try to explain it a  
little better.  
Right now the (pseudo)code on the “page” looks like that:  
  
get\_all\_texts  
get\_all\_pictures  
get\_all\_assets

The thing is that these elements should come out ordered, so it look  
for example like this:  
  
text\_1  
picture\_2  
asset\_1  
picture\_1

I hope that explains it a little bit better.

On Jan 22, 10:54 pm, Ilan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Chris-Aix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@Chris-Aix](https://www.ruby-forum.com/u/Chris-Aix)\
**Post date:** [January 23, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/problem-with-creating-a-database-scheme-model/126864/6 "2008-01-23T16:35:59Z")

</div>

Chris-Aix wrote:

> ok. i read the stuff about eager loading and it helped me out a little  
> bit.
> 
> right now i am doing something like this:  
> page = Page.find(:first, :include =\> [:pictures,:assets, :texts])
> 
> so this query gets all the results i need. the problem is each of this  
> tables has a position field and the endresult should be ordered this  
> way.  
> i made another scribble to explain it better.
> 
> [http://herrbuerger.com/scribble\_list.001.jpg](http://herrbuerger.com/scribble_list.001.jpg)
> 
> any ideas?

irb(main):001:0\> elements = []  
=\> []  
irb(main):002:0\> 10.times {|n| elements \<\< %w{text picture  
asset}.sort\_by {rand}.shift + "_" + n.to\_s}  
=\> 10  
irb(main):003:0\> elements.sort\_by {rand}  
=\> [“picture\_7”, “asset\_5”, “text\_3”, “text\_6”, “picture\_0”,  
“picture\_8”, “text\_2”, “text\_9”, “picture\_4”, “text\_1”]  
irb(main):004:0\> elements.sort\_by {|a| a.match(/_(\d+)$/)[1].to\_i}  
=\> [“picture\_0”, “text\_1”, “text\_2”, “text\_3”, “picture\_4”, “asset\_5”,  
“text\_6”, “picture\_7”, “picture\_8”, “text\_9”]
