Help me, I stuck

I have three tables and is not allowed to change data scheme:

CREATE TABLE applicants (
id int(10) unsigned NOT NULL auto_increment,
fullname varchar(60) NOT NULL default ‘’,
address varchar(60) NOT NULL default ‘’,
PRIMARY KEY (id)
) ;

CREATE TABLE positions (
id int(10) unsigned NOT NULL auto_increment,
position_name varchar(30) NOT NULL default ‘’,
KEY id (id)
) ;

CREATE TABLE sums (
id int(11) NOT NULL auto_increment,
applicants_id int(10) NOT NULL,
positions_id int(10) NOT NULL,
PRIMARY KEY (id)
) ;


I installed:

class Sum < ActiveRecord::Base
has_many :applicants
has_many :positions
end

class Applicant < ActiveRecord::Base
belongs_to :sum
end

class Position < ActiveRecord::Base
belongs_to :sum
end

At first I want to list all positions and then having clicked on it - to
receive all applicants with this position.

class WelcomeController < ApplicationController
def index
@positions = Position.find(:all)
end
end

welcome

Click on a position name to see all of applicants.

    <% @positions.each do |position| %>
  • <%= link_to "#{position.position_name} ", :controller => "applicant", :action => "show", :id => position.id %>
  • <% end %>
----------------------- and my question is - how to code applicant controller and show method not using SQL query directly.

‘show’ method should be:

def show(id)
sum = Sum.find(id)
@applicants = sum.applicants
end

Then in show.rhtml view you have:

<% for each applicant in @applicants %>
<% applicant.fullname %>
<% end %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs