Skip to content

Instantly share code, notes, and snippets.

@dan-mi-sun
Created July 5, 2014 18:54
Show Gist options
  • Save dan-mi-sun/9ec0b03021d2641b9d48 to your computer and use it in GitHub Desktop.
Save dan-mi-sun/9ec0b03021d2641b9d48 to your computer and use it in GitHub Desktop.
Display relevant projects for a creative based on matching skills
<% user_skills = [] %>
<% @user.skills.each do |skill| %>
<% user_skills << skill %>
<% end %>
<% @projects.each do |project| %>
<% project_skill = [] %>
<% project_skill << project.skills %>
<% if (user_skills & project_skill).length > 0 %>
<%= project.title %>
<% end %>
<% end %>
@dmgarland
Copy link

I see what you're trying to do; but don't forget that all of this logic should not be in the view: under MVC we want the view to just display something; not arrive at the information that we are displaying. This should be in the model as a method and called from the controller, leaving you something to iterate over in the view.

Lines 1-4 don't seem necessary to me. What's wrong with @user.skills?
In terms of finding the projects where a certain criteria is found, this is a use for the where method. Try something along the lines of

project.skills.where(:skill => @user.skills.ids)

Not 100% sure that will work, but the worst case scenario is you could have some kind of loop where you cross-reference the skills in a method. But do it in the model, and write a test for it.

Keep going!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment