【问题标题】:failure with belongs_to active record association - id returns nilbelongs_to 活动记录关联失败 - id 返回 nil
【发布时间】:2013-05-22 16:01:01
【问题描述】:

我想我知道问题出在哪里,但我似乎不知道如何解决它。

这是我的models

用户

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

  has_many :student_groups
...
end  

学生组

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 我有以下测试EDITED

...

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 都没有与主题相关联……我在这里做错了什么?谢谢!

【问题讨论】:

  • 为什么该科目会与该学生相关联?您是否希望它从学生组中挑选一个学生并自动将其分配给该主题?
  • 同意@FrederickCheung,模型有问题。主题和组之间不需要直接关系。他们已经有了桥梁“学生”。
  • 啊,对。所以...我应该在这里使用直通关联吗?对不起,我对协会有点困惑。感谢您指出这一点!
  • @BillyChan,所以我应该把 student_id 一起拿出来?

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


【解决方案1】:

重新加载@subject,可能它没有从数据库加载,因此它是空的

【讨论】:

    【解决方案2】:

    想通了。

    将模型更改为以下:

    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 的 cmets。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      相关资源
      最近更新 更多