Reputation: 91
class SubjectTeachersController < ApplicationController
def new
@st = SubjectTeacher.new
@tnames = Teacher.pluck(:tname)
@subs = Subject.pluck(:sub_name)
end
def create
@tech = Teacher.find(params[:tname]).id
@sub = Subject.find(params[:sub_name]).id
@st = SubjectTeacher.create(:teacher_id => @tech, :subject_id => @sub )
if @st.save
flash[:notice] = " creates successfully"
else
render('new')
end
end
end
I created mamy to many relationship between Subject & Teacher. SubjectTeacher is join table of Subject & Teacher. In SubjectTeacherController.
here, @tnames stores all the teacher name while @subs stores all subjects name. I want to access the id of both in controller...HOW? Is any changes shall I do in create function? plz tel me...
Upvotes: 1
Views: 52
Reputation: 5791
Rails provides 'magic' finder methods, of the form Model.find_by_attribute
.
In this case, we can use Teacher.find_by_tname(params[:tname])
and Subject.find_by_sub_name(params[:subname])
Upvotes: 1