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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Hallstrom <r...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> wrote:> > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 17, 9:40 am, Philip Hallstrom <r...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> wrote:> > 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 :) > > -philipSorry, 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
chris johnson wrote:> On Apr 17, 9:40 am, Philip Hallstrom <r...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 18, 2:15 pm, Josh Susser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> chris johnson wrote: > > On Apr 17, 9:40 am, Philip Hallstrom <r...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> 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 Susserhttp://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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---