开发者

Rails - many associations in one view

开发者 https://www.devze.com 2023-03-13 19:58 出处:网络
Thanks to stackoverflow users, stackoverflow helped me alot for my project until now. Now I face a new problem.

Thanks to stackoverflow users, stackoverflow helped me alot for my project until now. Now I face a new problem.

I already implemented this project http://img7.imagebanana.com/img/cnb46ti2/relationships.png (see Rails - data view from has_many and Has_many_and_belongs_to_many). 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.

Now I want to add 2 new tables.

  1. current_qualification http://img7.imagebanana.com/img/bimfgvut/UebersichtQualifikationenbistqualifi.png
  2. expected_qualification http://img7.imagebanana.com/img/72p0yj27/Ue开发者_开发技巧bersichtQualifikationencsollqualif.png

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

Question 1: 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.

Question 2: 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?

  1. I created the table 'current_qualification' with rails generate scaffold current_qualification employee_id:integer skill_id:integer qualificationstars:integer

  2. I added code into the models for has_many :through association

  3. I filled the table for current_qualification

  4. and now the tricky part: The employee controller! Because I already have an has_and_belongs_to_many association, the controller looks like this: def show @employee = Employee.find(params[:id], :include => { :position => :skills }) end .... How do I add :include => { :employee => { :current_qualifications => :skills} } 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?

Thanks in advance! And sorry for so much questions.

0

精彩评论

暂无评论...
验证码 换一张
取 消