# Rails associations

**URL:** <https://www.ruby-forum.com/t/rails-associations/154172>\
**Category:** RSpec\
**Created:** [November 26, 2008, 4:08am UTC](https://www.ruby-forum.com/t/rails-associations/154172 "2008-11-26T04:08:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@David\_Parker](https://www.ruby-forum.com/u/David_Parker)\
**Post date:** [November 26, 2008, 4:08am UTC](https://www.ruby-forum.com/t/rails-associations/154172/1 "2008-11-26T04:08:11Z")

</div>

Hello!

So I’m having some problems working out some probably really easy  
associations in Rails. I’ve Googled around and read some things on  
different Rails forums and blogs, but I just haven’t seen many solid  
examples.

Anyway, my question is a basic “how do I use RSpec with stubs/mocks  
through  
associations for methods”…  
perhaps code would be more clear. Here’s my controller:

def create  
@article = current\_account.article.create(params[:article])  
respond\_to do |format|  
if @article.save  
flash[:success] = ‘Article was successfully saved.’  
format.html { redirect\_to(accounts\_article\_path(@article)) }  
else  
format.html { render :action =\> “new” }  
end  
end  
end

And here is my corresponding spec:

describe “handling POST /accounts/articles” do  
before(:each) do  
@article = mock\_model(Article, :to\_param =\> ‘1’)  
@account = mock\_model(Account)  
controller.stub!(:current\_account).and\_return(@account)  
@account.stub!(:articles).and\_return(@article)  
@article.should\_receive(:create)  
end

```
describe "with successful save" do
  def do_post
    @article.should_receive(:save).and_return(true)
    post :create, :article => {}
  end

  it "should redirect to the new article" do
    do_post
    response.should redirect_to(accounts_article_url(@article))
  end
end

describe "with failed save" do
  def do_post
    @article.should_receive(:save).and_return(false)
    post :create, :article => {}
  end

  it "should re-render 'new'" do
    do_post
    response.should render_template('new')
  end
end

```

end

I’m getting a couple of errors from this and I can’t tell why. I have  
something similar done for non-associated models with no problem.  
NoMethodError in ‘Accounts::ArticlesController handling POST  
/accounts/articles with failed save should re-render ‘new’’  
You have a nil object when you didn’t expect it!

I’m sure that speccing associations can’t be that bad… I just don’t  
really  
know what to look for.

Dave, I can’t wait for your book to come out!

Thanks for the help!

David Parker

---

<div class="post-metadata">

**Author:** ![David\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@David\_Parker](https://www.ruby-forum.com/u/David_Parker)\
**Post date:** [November 26, 2008, 4:40am UTC](https://www.ruby-forum.com/t/rails-associations/154172/2 "2008-11-26T04:40:56Z")

</div>

So playing around with things, it appears I didn’t remember to put in  
the  
and\_return(@article)… so I changed my before to look something like  
this:

describe “handling POST /accounts/article” do  
before(:each) do  
@article = mock\_model(Article, :to\_param =\> ‘1’)  
@account = mock\_model(Account)  
controller.stub!(:current\_account).and\_return(@account)  
@account.stub!(:articles).and\_return(@article)  
Article.stub!(:new).and\_return(@article)  
@article.should\_receive(:new).and\_return(@article)  
end

Now, I still don’t entirely understand what I’m doing, so if someone can  
explain it or point me to a good blog article, I’d greatly appreciate  
it.  
Like I said before though, I’ve looked around and read over the  
documentation several times, but for some reason, my mind just isn’t  
grasping the associations ☹

David Parker

> **[David W Parker | Developer](https://www.davidwparker.com/)**
>
> David W Parker: a blog about web development,
> mobile development, game development,
> graphics, software, and entrepreneurship. I cover technologies such as Svelte, WebGL, OpenGL, Ruby,
> Ruby on Rails, Python, Django, JavaScript and more.

---

<div class="post-metadata">

**Author:** ![David\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@David\_Parker](https://www.ruby-forum.com/u/David_Parker)\
**Post date:** [November 26, 2008, 5:44am UTC](https://www.ruby-forum.com/t/rails-associations/154172/3 "2008-11-26T05:44:03Z")

</div>

On 2008-11-25, at 22:07, David Parker wrote:

> end  
> end  
> end  
> response.should render\_template(‘new’)  
> I’m sure that speccing associations can’t be that bad… I just  
> don’t really know what to look for.
> 
> Dave, I can’t wait for your book to come out!
> 
> Thanks for the help!
> 
> David Parker

Hi David. To write specs for ActiveRecord associations, I find Matthew  
Heidemann’s #stub\_association! extremely helpful. There was a  
discussion about it last week, actually:  
[Mocking and stubbing model relationships - RSpec - Ruby-Forum](http://www.ruby-forum.com/topic/171076)

I hope that helps. Cheers,  
Nick

---

<div class="post-metadata">

**Author:** ![David\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@David\_Parker](https://www.ruby-forum.com/u/David_Parker)\
**Post date:** [November 27, 2008, 1:14am UTC](https://www.ruby-forum.com/t/rails-associations/154172/4 "2008-11-27T01:14:06Z")

</div>

Nick H. wrote:

> Hi David. To write specs for ActiveRecord associations, I find Matthew  
> Heidemann’s #stub\_association! extremely helpful. There was a  
> discussion about it last week, actually:  
> [Mocking and stubbing model relationships - RSpec - Ruby-Forum](http://www.ruby-forum.com/topic/171076)
> 
> I hope that helps. Cheers,  
> Nick

That’s actually really helpful. Thanks Nick.

---

<div class="post-metadata">

**Author:** ![David\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d26b3c/32.png) [@David\_Parker](https://www.ruby-forum.com/u/David_Parker)\
**Post date:** [November 26, 2008, 4:46am UTC](https://www.ruby-forum.com/t/rails-associations/154172/5 "2008-11-26T04:46:57Z")

</div>

hi Parker, maybe it is not an association mock problem. I view your code  
and I think you need to correct one place

@account.stub!(:articles).and\_return(@article)

#=\>

@account.stub!(:article).and\_return(@article)
