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