简体   繁体   English

Ruby on Rails:如何插入具有从数据库中获取值的记录

[英]Ruby on Rails : How to insert record with fetching value from database

There are 3 tables : Student, College and Opted_College 有3个表:Student,College和Opted_College

I have a page say, Select College page(Add Opted Colleges page). 我有一个页面,说“选择大学”页面(“添加选择的大学”页面)。

A Student will log into the application , and opt for different colleges. 学生将登录该应用程序,然后选择其他学院。 This college list is fetched from "College" table. 该大学列表是从“学院”表中获取的。 Once the college is selected , the details like : ( college_id + student_id )should be added in the Opted_College table. 一旦选择了学院,应该在Opted_College表中添加诸如:(college_id + student_id)之类的详细信息。 For instatnce the Opted_College value might look like : 为了保持不变,Opted_College值可能类似于:

id College_id student_id 1 2 4 id College_id student_id 1 2 4

2 - RV College (college name) 4 - TestStudent (Student name) 2-RV学院(学院名称)4-TestStudent(学生名称)

I have not added a field for Student ID in the Select College page. 我尚未在“选择学院”页面中为“学生证”添加字段。 I need to capture the student ID from the session and this data needs to be added to Opted_College table. 我需要从会话中捕获学生ID,并且需要将此数据添加到Opted_College表中。

But after the Select College page is successfully submited, "NULL" value is entered for Student_ID column in the Opted_College table. 但是,在成功提交“选择学院”页面之后,在Opted_College表的Student_ID列中输入了“ NULL”值。

Please help. 请帮忙。

In the create method of your OptedColleges controller you want something like this: 在OptedColleges控制器的create方法中,您需要这样的内容:

@optedcollege.student_id = current_user.id
@optedcollege.save

I am of course assuming you are using an authentication solution that provides a current_user helper method... 我当然假设您使用的身份验证解决方案提供了current_user帮助程序方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM