title: Rspec testing Belongs to and has many
body: <p>I'm running a rspec test to make sure that two models are associated between each other with <code>has_many</code> and <code>belongs_to</code>. Here is my test below.</p>
  
  <pre><code>describe "testing for has many links" do
    before do
      @post = Post.new(day: "Day 1", content: "Test")
      @link = Link.new(post_id: @post.id, title: "google", url: "google.com")
    end
  
    it "in the post model" do
      @post.links.first.url.should == "google.com"
    end
  end
  </code></pre>
  
  <p>The test is telling me that url is an undefined method. What's wrong with my test? Or did I just miss something basic.</p>
  
  <p>The model file for Post</p>
  
  <pre><code>has_many :links
  </code></pre>
  
  <p>The model file for Link</p>
  
  <pre><code>belongs_to :post
  </code></pre>
  
  <p>On top of that, the link model has the attribute <code>post_id</code></p>