Created
October 25, 2013 22:03
-
-
Save coreyward/7162518 to your computer and use it in GitHub Desktop.
Rather than using a Postgres string array and doing selection in Ruby, utilize ActiveRecord and SQL to only retrieve the relevant data in the first place. Feedback on https://github.com/ros3bud/Meet2Talk/tree/54f02428ada5e913c03b2b58160a19ea6fd7e7f8.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db/migration/2013_add_skills_users_join_table.rb | |
class AddSkillsUsersJoinTable < ActiveRecord::Migration | |
# see http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association | |
def change | |
create_table :skills_users do |t| | |
t.belongs_to :skill | |
t.belongs_to :user | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/skill.rb | |
class Skill < ActiveRecord::Base | |
has_and_belongs_to_many :users | |
def self.list | |
# see http://guides.rubyonrails.org/active_record_querying.html#pluck | |
pluck(:name) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
class User < ActiveRecord::Base | |
# see http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association | |
has_and_belongs_to_many :skills | |
def self.with_skill(skill_name) | |
# see http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations | |
# see http://guides.rubyonrails.org/active_record_querying.html#hash-conditions | |
includes(:skills).where(skill: { name: skill_name }) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment