Ruby on rails 属于活动记录关联的失败-id返回零
我想我知道这里的问题是什么,但我似乎不知道如何解决它 这是我的Ruby on rails 属于活动记录关联的失败-id返回零,ruby-on-rails,ruby-on-rails-3,activerecord,rspec,Ruby On Rails,Ruby On Rails 3,Activerecord,Rspec,我想我知道这里的问题是什么,但我似乎不知道如何解决它 这是我的型号 使用者 class User < ActiveRecord::Base attr_accessor :password attr_accessible :name, :email, :password, :password_confirmation has_many :student_groups ... end 我在其他规范中进行了相同的测试,效果很好-我在控制台中进行了检查,得到了以下结果: 2.0
型号
使用者
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
has_many :student_groups
...
end
我在其他规范中进行了相同的测试,效果很好-我在控制台中进行了检查,得到了以下结果:
2.0.0-p0 :015 > @subject
=> #<Subject id: 1, name: "English", student_group_id: 1, student_id: nil, end_date: "2013-11-18", created_at: "2013-05-22 15:08:44", updated_at: "2013-05-22 15:08:44">
2.0.0-p0:015>@subject
=> #
因此,不管出于什么原因,学生id没有与主题联系起来……我在这里做错了什么?谢谢 Reload@subject,可能它不是从数据库加载的,因此它是空的 将型号更改为以下型号: 学生组。rb
class StudentGroup < ActiveRecord::Base
attr_accessible :name
belongs_to :user
has_many :students
end
class Student < ActiveRecord::Base
attr_accessible :gender, :name
belongs_to :student_group
has_many :subjects
end
class Subject < ActiveRecord::Base
attr_accessible :end_date, :name
belongs_to :student
end
require 'spec_helper'
describe StudentGroup do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@user.student_groups.create!(@attr).should be_valid
end
describe "User associations" do
it "should have a user attribute" do
@student_group.should respond_to(:user)
end
it "should have the right associated user" do
@student_group.user_id.should == @user.id
@student_group.user.should == @user
end
end
describe "Student associations" do
it "should have a student attritube" do
@student_group.should respond_to(:students)
end
end
end
require 'spec_helper'
describe Student do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@student_group.students.create!(@attr).should be_valid
end
describe "Student_Group associations" do
it "should have a student_group attribute" do
@student.should respond_to(:student_group)
end
it "should have the right associated student_group" do
@student.student_group_id.should == @student_group.id
@student.student_group.should == @student_group
end
end
describe "Subject associations" do
it "should have a subject attribute" do
@student.should respond_to(:subjects)
end
end
end
require 'spec_helper'
describe Subject do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@student.subjects.create!(@subject_attr).should be_valid
end
describe "Student associations" do
it "should have a student attribute" do
@subject.should respond_to(:student)
end
it "should have the right associated student" do
@subject.student_id.should == @student.id
@subject.student.should == @student
end
end
end
学生规范rb
class StudentGroup < ActiveRecord::Base
attr_accessible :name
belongs_to :user
has_many :students
end
class Student < ActiveRecord::Base
attr_accessible :gender, :name
belongs_to :student_group
has_many :subjects
end
class Subject < ActiveRecord::Base
attr_accessible :end_date, :name
belongs_to :student
end
require 'spec_helper'
describe StudentGroup do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@user.student_groups.create!(@attr).should be_valid
end
describe "User associations" do
it "should have a user attribute" do
@student_group.should respond_to(:user)
end
it "should have the right associated user" do
@student_group.user_id.should == @user.id
@student_group.user.should == @user
end
end
describe "Student associations" do
it "should have a student attritube" do
@student_group.should respond_to(:students)
end
end
end
require 'spec_helper'
describe Student do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@student_group.students.create!(@attr).should be_valid
end
describe "Student_Group associations" do
it "should have a student_group attribute" do
@student.should respond_to(:student_group)
end
it "should have the right associated student_group" do
@student.student_group_id.should == @student_group.id
@student.student_group.should == @student_group
end
end
describe "Subject associations" do
it "should have a subject attribute" do
@student.should respond_to(:subjects)
end
end
end
require 'spec_helper'
describe Subject do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@student.subjects.create!(@subject_attr).should be_valid
end
describe "Student associations" do
it "should have a student attribute" do
@subject.should respond_to(:student)
end
it "should have the right associated student" do
@subject.student_id.should == @student.id
@subject.student.should == @student
end
end
end
主题\u规范rb
class StudentGroup < ActiveRecord::Base
attr_accessible :name
belongs_to :user
has_many :students
end
class Student < ActiveRecord::Base
attr_accessible :gender, :name
belongs_to :student_group
has_many :subjects
end
class Subject < ActiveRecord::Base
attr_accessible :end_date, :name
belongs_to :student
end
require 'spec_helper'
describe StudentGroup do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@user.student_groups.create!(@attr).should be_valid
end
describe "User associations" do
it "should have a user attribute" do
@student_group.should respond_to(:user)
end
it "should have the right associated user" do
@student_group.user_id.should == @user.id
@student_group.user.should == @user
end
end
describe "Student associations" do
it "should have a student attritube" do
@student_group.should respond_to(:students)
end
end
end
require 'spec_helper'
describe Student do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@student_group.students.create!(@attr).should be_valid
end
describe "Student_Group associations" do
it "should have a student_group attribute" do
@student.should respond_to(:student_group)
end
it "should have the right associated student_group" do
@student.student_group_id.should == @student_group.id
@student.student_group.should == @student_group
end
end
describe "Subject associations" do
it "should have a subject attribute" do
@student.should respond_to(:subjects)
end
end
end
require 'spec_helper'
describe Subject do
before(:each) do
association_attr
end
it "should create a new instance with valid attributes" do
@student.subjects.create!(@subject_attr).should be_valid
end
describe "Student associations" do
it "should have a student attribute" do
@subject.should respond_to(:student)
end
it "should have the right associated student" do
@subject.student_id.should == @student.id
@subject.student.should == @student
end
end
end
最后将spec_helper.rb更改为以下内容:
def association_attr
# User attritbutes
@user = Factory(:user)
# Student_group
@student_group = @user.student_groups.create(@student_group_attr)
# Student_group attributes
@student_group_attr = { name: "4a"}
# Student
@student = @student_group.students.create(@student_attr)
# Student attributes
@student_attr = { name: "Example Student", gender: "Transgender" }
# Subject
@subject = @student.subjects.create!(@subject_attr)
# Subject attributes
@subject_attr = { name: "English", end_date: @date}
@date = Date.today+180
end
感谢并感谢您的评论。为什么该主题会与该学生关联?您是否希望它从学生组中选择一名学生并自动将其分配给主题?同意@FrederickCheung,模型有问题。主体和群体之间不需要直接关系。他们已经有了桥“学生”。啊,对。那么…我应该在这里使用直通关联吗?对不起,我被联想弄糊涂了。谢谢你指出这一点@BillyChan所以我应该把学生证一起带出去?