# Looking for feedback on my little domain model

**URL:** <https://www.ruby-forum.com/t/looking-for-feedback-on-my-little-domain-model/92269>\
**Category:** Rails\
**Created:** [April 17, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/looking-for-feedback-on-my-little-domain-model/92269 "2007-04-17T18:15:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_johnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chris\_johnson](https://www.ruby-forum.com/u/chris_johnson)\
**Post date:** [April 17, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/looking-for-feedback-on-my-little-domain-model/92269/1 "2007-04-17T18:15:50Z")

</div>

Hi there.

In my first rails app, a company can post multiple jobs (think job  
board).

My first instinct would be to say:  
class Company \< ActiveRecord::Base  
has\_many :jobs  
end  
class Job \< ActiveRecord::Base  
belongs\_to :company  
end

My question is of design: Should I use the above models, or does it  
make better sense to have a relation model, such as a JobPosting, that  
would make my code more like:  
class Company \< ActiveRecord::Base  
has\_many :jobs, :through =\> job\_postings  
end

I am just getting started, and I can foresee this sort of design  
decision arises regularly, so wanted to get some experienced feedback  
first.

I appreciate it.  
-chris

---

<div class="post-metadata">

**Author:** ![chris\_johnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chris\_johnson](https://www.ruby-forum.com/u/chris_johnson)\
**Post date:** [April 17, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/looking-for-feedback-on-my-little-domain-model/92269/2 "2007-04-17T18:29:52Z")

</div>

> My question is of design: Should I use the above models, or does it  
> make better sense to have a relation model, such as a JobPosting, that  
> would make my code more like:  
> class Company \< ActiveRecord::Base  
> has\_many :jobs, :through =\> job\_postings  
> end
> 
> I am just getting started, and I can foresee this sort of design  
> decision arises regularly, so wanted to get some experienced feedback  
> first.

The first one. Unless you can think of something that a JobPosting  
would  
have that a Job wouldn’t or for some reason want to assign the same Job  
to  
multiple Companies, but that just doesn’t make any sense to me 🙂

-philip

---

<div class="post-metadata">

**Author:** ![chris\_johnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chris\_johnson](https://www.ruby-forum.com/u/chris_johnson)\
**Post date:** [April 18, 2007, 5:34am UTC](https://www.ruby-forum.com/t/looking-for-feedback-on-my-little-domain-model/92269/3 "2007-04-18T05:34:57Z")

</div>

I appreciate your reply, and your reasoning makes sense to me.

There is one more case I am unsure of. Let’s say I have something  
like this:  
class Job \< ActiveRecord::Base  
has\_many :workers  
end  
class Worker \< ActiveRecord::Base  
belongs\_to :job  
end

In this case, would you consider using a relational model like this?  
class Assignment \< ActiveRecord::Base  
has\_many :workers  
belongs\_to :job  
end  
class Job \< ActiveRecord::Base  
has\_many :assignments  
has\_many :workers, :through =\> :assignments  
end  
class Worker \< ActiveRecord::Base  
belongs\_to :assignment  
end

Also, is this logical use of “has\_many :through”? I have a lot to  
learn, but am excited.

I appreciate any guidance.  
-Chris

---

<div class="post-metadata">

**Author:** ![chris\_johnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chris\_johnson](https://www.ruby-forum.com/u/chris_johnson)\
**Post date:** [April 18, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/looking-for-feedback-on-my-little-domain-model/92269/4 "2007-04-18T20:19:49Z")

</div>

On Apr 17, 9:40 am, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The first one. Unless you can think of something that a JobPosting would  
> have that a Job wouldn’t or for some reason want to assign the same Job to  
> multiple Companies, but that just doesn’t make any sense to me 🙂
> 
> -philip

Sorry, I realized that some point won’t read this unless my comments  
proceed (as opposed to precede) previous comments::  
I appreciate your reply, and your reasoning makes sense to me.

There is one more case I am unsure of. Let’s say I have something  
like this:  
class Job \< ActiveRecord::Base  
has\_many :workers  
end  
class Worker \< ActiveRecord::Base  
belongs\_to :job  
end

In this case, would you consider using a relational model like this?  
class Assignment \< ActiveRecord::Base  
has\_many :workers  
belongs\_to :job  
end  
class Job \< ActiveRecord::Base  
has\_many :assignments  
has\_many :workers, :through =\> :assignments  
end  
class Worker \< ActiveRecord::Base  
belongs\_to :assignment  
end

Also, is this logical use of “has\_many :through”? I have a lot to  
learn, but am excited.

I appreciate any guidance.  
-Chris

---

<div class="post-metadata">

**Author:** ![chris\_johnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chris\_johnson](https://www.ruby-forum.com/u/chris_johnson)\
**Post date:** [April 18, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/looking-for-feedback-on-my-little-domain-model/92269/5 "2007-04-18T23:15:01Z")

</div>

chris johnson wrote:

> On Apr 17, 9:40 am, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > The first one. Unless you can think of something that a JobPosting would  
> > have that a Job wouldn’t or for some reason want to assign the same Job to  
> > multiple Companies, but that just doesn’t make any sense to me 🙂
> > 
> > -philip
> 
> Sorry, I realized that some point won’t read this unless my comments  
> proceed (as opposed to precede) previous comments::  
> I appreciate your reply, and your reasoning makes sense to me.
> 
> There is one more case I am unsure of. Let’s say I have something  
> like this:  
> class Job \< ActiveRecord::Base  
> has\_many :workers  
> end  
> class Worker \< ActiveRecord::Base  
> belongs\_to :job  
> end
> 
> In this case, would you consider using a relational model like this?  
> class Assignment \< ActiveRecord::Base  
> has\_many :workers  
> belongs\_to :job  
> end  
> class Job \< ActiveRecord::Base  
> has\_many :assignments  
> has\_many :workers, :through =\> :assignments  
> end  
> class Worker \< ActiveRecord::Base  
> belongs\_to :assignment  
> end
> 
> Also, is this logical use of “has\_many :through”? I have a lot to  
> learn, but am excited.

That is a perfectly valid use of hmt, chaining two 1-to-many relations  
into a many-to-many. It can make it a bit indirect to find a worker’s  
job - you have to say worker.assignment.job to get there. However, that  
will break down if a worker works more than one job. In that case,  
you’ll want assignments to be a standard issue join table like so:

class Job \< ActiveRecord::Base  
has\_many :assignments  
has\_many :workers, :through =\> :assignments  
end  
class Assignment \< ActiveRecord::Base  
belongs\_to :worker  
belongs\_to :job  
end  
class Worker \< ActiveRecord::Base  
has\_many :assignments  
has\_many :jobs, :through =\> :assignments  
end

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![chris\_johnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chris\_johnson](https://www.ruby-forum.com/u/chris_johnson)\
**Post date:** [April 19, 2007, 2:01am UTC](https://www.ruby-forum.com/t/looking-for-feedback-on-my-little-domain-model/92269/6 "2007-04-19T02:01:18Z")

</div>

On Apr 18, 2:15 pm, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > proceed (as opposed to precede) previous comments::
> 
> > belongs\_to :assignment
> 
> has\_many :jobs, :through =\> :assignments  
> end
> 
> –  
> Josh S.[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Josh, many thanks. Your example with the standard join table helped  
me to look at my models in a different and more accurate context.

BTW, I enjoy your blog.  
Cheers,  
Chris
