Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - many associations in one view
    text
    copied!<p>Thanks to stackoverflow users, stackoverflow helped me alot for my project until now. Now I face a new problem. </p> <p>I already implemented this project <a href="http://img7.imagebanana.com/img/cnb46ti2/relationships.png" rel="nofollow noreferrer">http://img7.imagebanana.com/img/cnb46ti2/relationships.png</a> (see <a href="https://stackoverflow.com/questions/6390262/rails-data-view-from-has-many-and-has-many-and-belongs-to-many">Rails - data view from has_many and Has_many_and_belongs_to_many</a>). For connecting positions and skills, I made an has_and_belongs_to_many relationship, because every position has many skills and every skills has many positions and for this table I didn't want to add some columns. The table 'positions_skills' puts both together. </p> <p>Now I want to add 2 new tables. </p> <ol> <li><strong>current_qualification</strong> <a href="http://img7.imagebanana.com/img/bimfgvut/UebersichtQualifikationenbistqualifi.png" rel="nofollow noreferrer">http://img7.imagebanana.com/img/bimfgvut/UebersichtQualifikationenbistqualifi.png</a> </li> <li><strong>expected_qualification</strong> <a href="http://img7.imagebanana.com/img/72p0yj27/UebersichtQualifikationencsollqualif.png" rel="nofollow noreferrer">http://img7.imagebanana.com/img/72p0yj27/UebersichtQualifikationencsollqualif.png</a></li> </ol> <p>if it's not clear what these tables with the qualification stars do: Every skill in a position has it's own level of qualification (for example for Position 1, you need to be competent in skill 1 alot (5 stars), but for Position 2, you need not so much knowledge about this skill(2 stars)). -> expected_qualification. And every employee is different good in each skill -> current_qualification</p> <p><strong>Question 1:</strong> The table 'expected_qualification' seems similar to the table 'positions_skills'. Am I right that if I add the table 'expected_qualification', than 'position_skills' is not necessary anymore? I just used 'position_skills' for connecting this n:m relationship for viewing employee's skills on show.html.erb, but it's also possible with 'has_many :through' associations if I understood right.</p> <p><strong>Question 2:</strong> Both tables are necessary for viewing these little stars on the view of employee's show.html.erb. (as you can see on my pictures. But right now I'm satisfied with numbers instead of Stars => 4 stars = 4 as integer in column). What did I do? </p> <ol start="3"> <li><p>I created the table <strong>'current_qualification'</strong> with <code>rails generate scaffold current_qualification employee_id:integer skill_id:integer qualificationstars:integer</code> </p></li> <li><p>I added code into the models for has_many :through association</p></li> <li><p>I filled the table for current_qualification </p></li> <li><p>and now the tricky part: The employee controller! Because I already have an has_and_belongs_to_many association, the controller looks like this: <code>def show @employee = Employee.find(params[:id], :include =&gt; { :position =&gt; :skills }) end ...</code>. How do I add <code>:include =&gt; { :employee =&gt; { :current_qualifications =&gt; :skills} }</code> for the has_many :through relation now into the already existing code? And which code lets me output the qualificationstars-column from the current_qualification-table for show.html.erb of employee? </p></li> </ol> <p>Thanks in advance! And sorry for so much questions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload