Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 4 accessing model attribute
    text
    copied!<p>I'm learning rails (I'm mostly a java developer), as will be obvious by my question, and am having trouble accessing a method on my model from my rspec test. I'm basically following <a href="http://ruby.railstutorial.org/book/ruby-on-rails-tutorial" rel="nofollow">this tutorial</a> but applying it to my own project. I'm trying to define graduation_year as a method based on the grade of the student and the current time of the year. When I instantiate the object and call the method in rails console it works as I'd expect, but when trying to do it in a test I'm getting:</p> <pre><code>undefined method `graduation_year' </code></pre> <h2>student.rb</h2> <pre><code>class Student &lt; ActiveRecord::Base validates :school_id, presence: true, uniqueness: true validates :first_name, presence: true, length: { maximum: 30 } validates :last_name, presence: true, length: { maximum: 40 } validates :gender, presence: true, inclusion: { :in =&gt; %w(m f M F) } validates :grade, presence: true, numericality: { greater_than_or_equal_to: 1 , less_than_or_equal_to: 12 } def graduation_year year = Date.today.year + ( 12 - grade ) end end </code></pre> <h2>student_spec.rb</h2> <pre><code>require 'spec_helper' describe Student do before { @student = Student.new( school_id: 3, first_name: "Michael", last_name: "Green", gender: "M", grade: 6)} subject { @student } it { should respond_to(:school_id) } it { should respond_to(:first_name) } it { should respond_to(:last_name) } it { should respond_to(:gender) } it { should respond_to(:grade) } it { should respond_to(:graduation_year) } it { should be_valid } describe "when school_id is not present" do before { @student.school_id = " " } it { should_not be_valid } end . . . describe "when grade is 7 graduation_year should be calculated correctly" do before { @student.grade = 7 } expect(@student.graduation_year).to eq(Date.today.year+5) end end </code></pre> <h2>Error message</h2> <pre><code>~/dev/rails_apps/spec/models/student_spec.rb:96:in `block (2 levels) in &lt;top (required)&gt;': undefined method `graduation_year' for nil:NilClass (NoMethodError) from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `module_eval' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `subclass' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:228:in `describe' from ~/dev/rails_apps/spec/models/student_spec.rb:94:in `block in &lt;top (required)&gt;' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `module_eval' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:242:in `subclass' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:228:in `describe' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/dsl.rb:18:in `describe' from ~/dev/rails_apps/spec/models/student_spec.rb:3:in `&lt;top (required)&gt;' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run' from ~/dev/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun' </code></pre> <p>Also, I don't think this is the issue, but I originally had the <code>graduation_year</code> as an attribute on the Student table, but removed it and this test is supposed to verify that it is calculated correctly.</p> <p>I've also tried <code>puts first_name</code> in the tests and I get the same undefined method error but on <code>first_name</code>. I think I'm just missing something about the way ruby/rails works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload