Flint285
Flint285

Reputation: 5

A Ruby on Rails has_many relationship.

I'm new to RoR and working on my first project. The basic concept behind the idea is to connect "Users" that have chosen a set a "Skills" with other Users that that have submitted a "Help Request" that deals specifically with those chosen skills. An app that connects Skilled Users with Users that need help if you will. My question has to do with the relationship between the Users, Skills, and Help_Request Models. It feels like a "has_many :through association" or maybe "polymorphic association" might be in order for this kind of three way relationship? Really not sure?

Any thoughts or suggestions would be greatly appreciated.

Upvotes: 0

Views: 283

Answers (3)

user419017
user419017

Reputation:

I'm afraid ShivamD's answer will not suffice. That would require duplicate Skills, and queries the relevant User.help_requests via relationship, where instead you need an intersection.

Not just that, Users who create HelpRequests will already have a has_many relationship. That makes sense.

I won't include schema for the HABTM I'm about to suggest (see here), but I will cover the models. Essentially what you want:

skill = Skill.create(name: "My Skill")
user.skills         << skill
help_request.skills << skill

user.matched_help_requests
#> [help_request]

Which can be achieved as follows:

class User
  has_and_belongs_to_many :skills

  def matched_help_requests
    HelpRequest.joins(:skills).where("skills.id IN(?)", skills.pluck(:id))
  end
end

class HelpRequest
  has_and_belongs_to_many :skills
end

class Skill
  has_and_belongs_to_many :users
  has_and_belongs_to_many :help_requests
end

EDIT: here's the schmea for the HABTM:

rails g migration add_skills_join_tables

within migration

def change
  create_table :skills_users, id: false do |t|
    t.references :skill
    t.references :user
  end

  create_table :help_requests_skills, id: false do |t|
    t.references :skill
    t.references :help_request
  end

  add_index :skills_users,        [:skill_id, :user_id]
  add_index :help_request_skills, [:skill_id, :help_request_id] 
end

Upvotes: 0

tiagotex
tiagotex

Reputation: 56

One hay is to create a has_and_belongs_to_manyrelation between the Users and Helprequests and Skills, so you end up with this:

class User < ActiveRecord::Base
  has_and_belongs_to_many :skills
end

class Help_request < ActiveRecord::Base
  has_and_belongs_to_many :skills
end

class Skills < ActiveRecord::Base
      has_and_belongs_to_many :users
      has_and_belongs_to_many :help_requests
end

Then you need to create the tables

 rails g migration add_skills_users_table

 rails g migration add_help_requests_skills_table

In the end run rake db:migrate

You can then search for it using User.first.skills

Upvotes: 0

ShivamD
ShivamD

Reputation: 961

A polymorphic association is when a model should belong to another model. Let's say when you a comment model. You can comment on a post and a comment itself. That's when you would use a polymorphic.

In your case a simple has_many through would do. It should look like this

class User < ActiveRecord::Base 
  has_many :skills 
  has_many :help_requests, through: :skills
end

class Skill < ActiveRecord::Base 
  belongs_to :user
  belongs_to :helpRequest
end

class HelpRequest < ActiveRecord::Base 
  has_many :skills 
  has_many :users, through :skills
end

For more information the docs

Upvotes: 1

Related Questions