gkr
gkr

Reputation: 79

Rails Model Association Confusion

I am building a app(to learn rails) which allows companies/(individuals too) to create a profile page to list their employees and skills so a user to my site able to find people based on the skill so he/she can hire them as a contract employee for a period of time.

I have these models company,employee,skill,contract but i am confused about how to make associations between these models to do what i want.

Is there any open source projects similar to this exist so i can learn from it.

Upvotes: 1

Views: 594

Answers (2)

David
David

Reputation: 844

Sounds like you need:

class Company < ActiveRecord::Base
  has_many :employees
end

class Employee < ActiveRecord::Base
  has_many :employeeskills
  has_many :skills, :through => :employeeskills
  belongs_to :company
  has_many :contracts
end

class Skill < ActiveRecord::Base
  has_many :employeeskills
  has_many :employees, :through => :employeeskills
end

class Employeeskill < ActiveRecord::Base
  belongs_to :employee
  belongs_to :skill
end

class Contract < ActiveRecord::Base
  belongs_to :employee
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :contracts
end

Then you can ask for @user.contracts or @employee.skills etc.

Hope that helps!

Upvotes: 2

twe4ked
twe4ked

Reputation: 2892

I'm not sure of any open source projects to look at but have you been through the Getting Started Rails Guide? It covers basic associations.

Upvotes: 1

Related Questions