belongs_to活动记录关联失败 - id返回nil

时间:2013-05-22 16:01:01

标签: ruby-on-rails ruby-on-rails-3 activerecord rspec

我想我知道这里的问题是什么,但我似乎无法弄清楚如何解决它。

这是我的models

用户

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :student_groups
...
end  

StudentGroup

class StudentGroup < ActiveRecord::Base
  attr_accessible :name

  belongs_to :user
  has_many   :subjects
  has_many   :students

end

主题

class Subject < ActiveRecord::Base
  attr_accessible :end_date, :name

  belongs_to :student_group
  belongs_to :student

end

学生

class Student < ActiveRecord::Base
  attr_accessible :gender, :name

  belongs_to :student_group
  has_many :subjects

end

在我的Student_Spec.rb我有以下测试已编辑

...

before(:each) do
  @user = Factory(:user)
  @student_group_attr = { name: "4a"}
  @student_group = @user.student_groups.create(@student_group_attr)
  @date = Date.today+180
  @subject_attr = { name: "English", end_date: @date}
end

...

describe "Student associations" do

  before(:each) do
    @subject = @student_group.subjects.create!(@subject_attr)
    @student_attr = { name: "Example Student", gender: "Female" }
    @student = @student_group.students.create(@student_attr) 
  end

  it "should have the right associated student" do
    @subject.student_id.should == @student.id
    @subject.student.should == @student
  end
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">

所以无论出于何种原因,student_id都没有与主题相关联......我在这里做错了什么?谢谢!

2 个答案:

答案 0 :(得分:0)

重新加载@subject,也许它没有从db加载,因此它是空的

答案 1 :(得分:0)

想出来。

将模型更改为以下内容:

<强> student_groups.rb

class StudentGroup < ActiveRecord::Base
  attr_accessible :name

  belongs_to :user
  has_many   :students

end

<强> students.rb

class Student < ActiveRecord::Base
  attr_accessible :gender, :name

  belongs_to :student_group
  has_many :subjects

end

<强> subject.rb中

class Subject < ActiveRecord::Base
  attr_accessible :end_date, :name

  belongs_to :student

end

模型规格如下:

<强> student_group_spec.rb

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

<强> student_spec.rb

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

<强> subject_spec.rb

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

感谢Frederick CheungBilly Chan的评论。