# Wondering about combining has\_one and has\_many in one application

**URL:** <https://www.ruby-forum.com/t/wondering-about-combining-has-one-and-has-many-in-one-application/184318>\
**Category:** Rails\
**Created:** [February 28, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/wondering-about-combining-has-one-and-has-many-in-one-application/184318 "2010-02-28T21:37:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [February 28, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/wondering-about-combining-has-one-and-has-many-in-one-application/184318/1 "2010-02-28T21:37:14Z")

</div>

I am writing an app for a foodbank. The edit screen uses a database  
called visit with fields of household\_id, month, year, monthly, and  
weekly. Household\_id is a link to another database with family  
records, month and year are for the date of this record, monthly is  
for a monthly visit, and weekly is for a weekly fruit and bread visit.  
In the edit screen the Household database has a has\_one relationship  
with Visit, so it only finds 1 record and asks about attendance this  
month. The problem comes when I try to make a report using has\_many.  
The report shows attendance for the calendar year.

I’m sure there is a way to do this with Rails, but I have no idea, so  
please help…

Bob

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [March 1, 2010, 10:09am UTC](https://www.ruby-forum.com/t/wondering-about-combining-has-one-and-has-many-in-one-application/184318/2 "2010-03-01T10:09:48Z")

</div>

On 28 February 2010 20:36, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am writing an app for a foodbank. The edit screen uses a database  
> called visit with fields of household\_id, month, year, monthly, and  
> weekly. Household\_id is a link to another database with family  
> records, month and year are for the date of this record, monthly is  
> for a monthly visit, and weekly is for a weekly fruit and bread visit.  
> In the edit screen the Household database has a has\_one relationship  
> with Visit, so it only finds 1 record and asks about attendance this  
> month. The problem comes when I try to make a report using has\_many.  
> The report shows attendance for the calendar year.

Relationships have nothing to do with the views you choose to display,  
but reflect the fundamental relationships of the real-world objects  
that you are mapping using the database. Think first about these  
models and their relationships and then build the views you want on  
top of that.

So for example if Household has\_many Visits then you may choose to  
only show one or ones occurring in a particular week, but that does  
not change the relationship itself.

Also it is unusual to use more than one database, or did you mean  
tables?

I do not know how much experience you have with Rails but if that is  
not a lot you may like to work through some of the guides at  
[http://guides.rubyonrails.org](http://guides.rubyonrails.org). There is one on ActiveRecord  
Relationships which may help. Also check out the others.

Colin
