# Model question

**URL:** <https://www.ruby-forum.com/t/model-question/178874>\
**Category:** Rails\
**Created:** [November 12, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/model-question/178874 "2009-11-12T20:06:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [November 12, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/model-question/178874/1 "2009-11-12T20:06:23Z")

</div>

Heyya,

I am working on a apartments rent model.

Some apartments are individual with address, price … fields and the  
model for this is streightforward.  
Some apartments are a collection of apts in a complex with the same  
address but different price, bedroom number …  
What is a good way to structure this difference.

I was thinking about adding to the Apartments model a “belongs to many”  
additional model. This child model would contain info on the apartments  
belonging to a multi unit complex. But searching by price range sounds  
little messy in this case, with duplicate addresses resolving and  
combing through apartments and child complex model for pricing

Any suggestions are very welcome

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [November 12, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/model-question/178874/2 "2009-11-12T20:34:13Z")

</div>

> additional model. This child model would contain info on the  
> apartments  
> belonging to a multi unit complex. But searching by price range  
> sounds  
> little messy in this case, with duplicate addresses resolving and  
> combing through apartments and child complex model for pricing
> 
> Any suggestions are very welcome

Perhaps have an Apartment model that is the complex. Then have a Unit  
model that has the details for that particular unit. This creates a  
little annoyance when you have an apartment complex that has only one  
unit, but at least it’s consistent.

As for search by price range… add a “min\_price” and “max\_price” to  
the Apartment model and then some after save handlers on the Unit  
model that would update those fields appropriately. That way a search  
by price is done only the the Apartment model and should be very  
quick…

-philip

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [November 12, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/model-question/178874/3 "2009-11-12T20:49:50Z")

</div>

Philip H. wrote:

> > additional model. This child model would contain info on the  
> > apartments  
> > belonging to a multi unit complex. But searching by price range  
> > sounds  
> > little messy in this case, with duplicate addresses resolving and  
> > combing through apartments and child complex model for pricing
> > 
> > Any suggestions are very welcome
> 
> Perhaps have an Apartment model that is the complex. Then have a Unit  
> model that has the details for that particular unit. This creates a  
> little annoyance when you have an apartment complex that has only one  
> unit, but at least it’s consistent.
> 
> As for search by price range… add a “min\_price” and “max\_price” to  
> the Apartment model and then some after save handlers on the Unit  
> model that would update those fields appropriately. That way a search  
> by price is done only the the Apartment model and should be very  
> quick…
> 
> -philip

Thanks Philip. That sounds like a clear, good way to do it. I initially  
thought of something like that. I strayed away because I already created  
a model for single house/condo rent and felt I’ll be re-doing a lot of  
code if I do multi type separately. But it really sounds like the right  
choice.

Cheers,  
mrak

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [November 12, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/model-question/178874/4 "2009-11-12T21:02:16Z")

</div>

What about “group\_by” association, I haven’t used it yet, is it a good  
idea to see if I can use it to group multi-unit apts by address?
