ruby - Association Issue -
i have 3 models user, club , mcq.
in club model. assign club (class) -
- 9-physics
- 9-chemistry
- 10-physics
- 10-chemistry...
here association
class user < activerecord::base has_and_belongs_to_many :clubs end class club < activerecord::base has_many :mcqs has_and_belongs_to_many :users end class mcq < activerecord::base belongs_to :club end
in student view (student show index) show club (as subject) , after click on subject want show mcq topic of related subject.
for club controller -
class clubscontroller < applicationcontroller #its show subject list def student_show_index @club = current_user.clubs end #its show topic according subject. def student_show_topic @club = current_user.clubs @mcq = @club.first.mcqs.order('created_at desc') end end
so question is, when click on subject physics show mcq of 9th. , same chemistry.
i want filtered mcq according subject.
you have send params
club_id
in link of subject clicking. eg. <%=link_to "subject", x_path(club_id: n) %>
can catch params in controller action params[:club_id]
. rewrite controller action following
def student_show_topic @club = club.find(params[:club_id]) @mcq = @club.mcqs.order('created_at desc') end
not may need permit club_id
params in controller, if not yet added. hope you. let me know if issues?
Comments
Post a Comment