Rails变量数据正在改变
Rails variable data is changing
我有两个 table 具有多对多关联
学生
项目
通过 table student_projects
连接
student = Student.find(1)
old_projects = student.projects
#Now Iam calling function that will create projects
create_projects
student = Student.find(1)
new_projects = student.projects
newly_added_projects = new_projects - old_projects
旧项目是空的,new_projects不是
我把记录器语句放在创建行之前和之后(new_projects = students.projects),然后我可以看到区别。
但是当我将记录器语句仅放在行后 (newly_added_projects = new_projects - old_projects) 检查 old_projects, new_projects, newly_added_projects
那么 old_projects 等于 new_projects
谁能帮我解决这个问题
student = Student.find(1)
old_projects = student.projects
newly_added_projects = create_projects # return newly projects from method itself.
def create_projects
projects = []
#add newly created projects in projects array and return at the end.
return projects
end
做那样的事情。无需从旧计算它,您已经在 create_projects 方法中进行了项目。
我建议你在创建新项目之前和之后收集项目 ID,然后减去旧项目 ID 和新项目 ID,如下所示,
student = Student.find(1)
old_project_ids = student.projects.map(&:id) # You may use student.project_ids
#Now Iam calling function that will create projects
create_projects
student = Student.find(1)
new_project_ids = student.projects.map(&:id)
newly_added_project_ids = new_project_ids - old_project_ids
否则,您可以将 old_projects 对象从活动记录关系转换为纯 ruby 数组,如下所示,
student = Student.find(1)
old_projects = student.projects.to_a
#Now Iam calling function that will create projects
create_projects
student = Student.find(1)
new_projects = student.projects_to_a
newly_added_projects = new_projects - old_projects
我有两个 table 具有多对多关联
学生 项目
通过 table student_projects
连接student = Student.find(1)
old_projects = student.projects
#Now Iam calling function that will create projects
create_projects
student = Student.find(1)
new_projects = student.projects
newly_added_projects = new_projects - old_projects
旧项目是空的,new_projects不是
我把记录器语句放在创建行之前和之后(new_projects = students.projects),然后我可以看到区别。
但是当我将记录器语句仅放在行后 (newly_added_projects = new_projects - old_projects) 检查 old_projects, new_projects, newly_added_projects
那么 old_projects 等于 new_projects
谁能帮我解决这个问题
student = Student.find(1)
old_projects = student.projects
newly_added_projects = create_projects # return newly projects from method itself.
def create_projects
projects = []
#add newly created projects in projects array and return at the end.
return projects
end
做那样的事情。无需从旧计算它,您已经在 create_projects 方法中进行了项目。
我建议你在创建新项目之前和之后收集项目 ID,然后减去旧项目 ID 和新项目 ID,如下所示,
student = Student.find(1)
old_project_ids = student.projects.map(&:id) # You may use student.project_ids
#Now Iam calling function that will create projects
create_projects
student = Student.find(1)
new_project_ids = student.projects.map(&:id)
newly_added_project_ids = new_project_ids - old_project_ids
否则,您可以将 old_projects 对象从活动记录关系转换为纯 ruby 数组,如下所示,
student = Student.find(1)
old_projects = student.projects.to_a
#Now Iam calling function that will create projects
create_projects
student = Student.find(1)
new_projects = student.projects_to_a
newly_added_projects = new_projects - old_projects