Sql and ruby

let me give some background knowledge on my problem

currently:

courses

programmes

degrees

are stored in the database, the app allows people to create courses and
programmes. Then they are allowed to assign courses and programmes
together, of course no duplicates allowed. The combinations are stored
in the degree table.

eg. Records in the degree table

Record 1:
course code: fg123
programme code: ha123

Record 2:
course code: yh123
programme code: ha123

Record 3:
course code: ji123
programme code: kp123

now the problem i have met with is that

i want to allow the user to click on a link, which displays all the
programme codes, i have grouped them together such that the user will
seeā€¦

Programmes:

-ha123
-kp123

and when they click on the link itself, the app will be able to display
all the course codes of courses which has that programme code

eg. when user clicks on ha123

Courses under ha123:

-fg123
-yh123

can anyone guide me on how to do this without injecting sql statements?