Looking for feedback on my little domain model

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

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 :slight_smile:

-philip

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

On Apr 17, 9:40 am, Philip H. [email protected] 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 :slight_smile:

-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

chris johnson wrote:

On Apr 17, 9:40 am, Philip H. [email protected] 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 :slight_smile:

-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

On Apr 18, 2:15 pm, Josh S. [email protected]
wrote:

proceed (as opposed to precede) previous comments::

belongs_to :assignment

has_many :jobs, :through => :assignments
end


Josh S.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