# Rails association not working. "Undefined method" error in trying to access child object from parent

**URL:** <https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682>\
**Category:** Rails\
**Created:** [April 8, 2012, 1:49pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682 "2012-04-08T13:49:37Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [April 8, 2012, 1:49pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/1 "2012-04-08T13:49:37Z")

</div>

For some reason (most likely in my view due to naming conventions) rails  
association is not working. Trying to access child object from parent  
object gives undefined method.

_Routes.rb:_  
\*  
\*

Sns::Application.routes.draw do  
root :to =\> “questions#index”  
resources :questions do  
resources :question\_responses  
end

- 
- 

_question.rb:_  
\*  
\*

class Question \< ActiveRecord::Base  
validates :title, :presence =\> true  
validates :description, :presence =\> true

has\_many :question\_responses, :dependent =\> :destroy, :class\_name =\>  
“QuestionResponse”  
accepts\_nested\_attributes\_for :question\_responses, :allow\_destroy =\>  
true  
end

- 
- 

_question\_response.rb:_  
\*  
\*

class QuestionResponse \< ActiveRecord::Base  
validates :body, :presence =\> true  
belongs\_to :question, :foreign\_key =\> “question\_id”  
end

- 
- 

_questions\_controller.rb:_  
\*  
\*

class QuestionsController \< ApplicationController  
before\_filter :find\_question  
before\_filter :find\_question\_response, :only =\> [:show, :edit,  
:update, :destroy]

def index  
@question = Question.new  
@questions = Question.all  
end

def new  
@question = Question.new  
@question\_response = @question.question\_responses.build  
end

def create  
@question = Question.new(params[:question])

```
if @question.save
  flash[:notice] = "Question has been created."
  redirect_to @question
else
  flash[:alert] = "Question has not been created."
  render :action => 'new'
end

@question_response = 

```

@question.question\_responses.build(params[:question\_response])  
if @question\_response.save  
flash[:notice] = “Reply has been created.”  
redirect\_to [@question, @question\_response]  
else  
flash[:alert] = “Reply has not been created.”  
render “question\_responses/new”  
end  
end

def show  
@question = Question.find(params[:id])  
end

def edit  
@question = Question.find(params[:id])  
end

def update  
@question = Question.find(params[:id])

```
if @question.update_attributes(params[:question])
  flash[:notice] = "Question has been updated."
  redirect_to @question
else
  flash[:alert] = "Question has not been updated."
  render :action => 'edit'
end

```

end

def destroy  
@question = Question.find(params[:id])  
@question.destroy  
flash[:notice] = “Question has been deleted.”  
redirect\_to questions\_path  
end

def find\_question\_response  
@question\_response = @question.question\_responses.find(params[:id])  
end

private  
def find\_question  
if (params[:question\_response] &&  
params[:question\_response][:question\_id])  
@question =  
Question.find(params[:question\_response][:question\_id])  
elsif params[:question\_id]  
@question = Question.find(params[:question\_id])  
end  
end  
end

- 
- 

_Error with trace:_  
\*  
\*

undefined method `question\_responses’ for nil:NilClass

app/controllers/questions\_controller.rb:55:in `find_question_response' activesupport (3.2.1) lib/active_support/callbacks.rb:429:in`\_run\_\_35842215\_\_process\_action\_\_9839693\_\_callbacks’  
activesupport (3.2.1) lib/active\_support/callbacks.rb:405:in  
`__run_callback' activesupport (3.2.1) lib/active_support/callbacks.rb:385:in`\_run\_process\_action\_callbacks’  
activesupport (3.2.1) lib/active\_support/callbacks.rb:81:in  
`run_callbacks' actionpack (3.2.1) lib/abstract_controller/callbacks.rb:17:in`process\_action’  
actionpack (3.2.1) lib/action\_controller/metal/rescue.rb:29:in  
`process_action' actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:30:in`block in process\_action’  
activesupport (3.2.1) lib/active\_support/notifications.rb:123:in `block in instrument' activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in`instrument’  
activesupport (3.2.1) lib/active\_support/notifications.rb:123:in  
`instrument' actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:29:in`process\_action’  
actionpack (3.2.1) lib/action\_controller/metal/params\_wrapper.rb:205:in  
`process_action' activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:18:in`process\_action’  
actionpack (3.2.1) lib/abstract\_controller/base.rb:121:in `process' actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in`process’  
actionpack (3.2.1) lib/action\_controller/metal.rb:203:in `dispatch' actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in`dispatch’  
actionpack (3.2.1) lib/action\_controller/metal.rb:246:in `block in action' actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in`call’  
actionpack (3.2.1) lib/action\_dispatch/routing/route\_set.rb:66:in  
`dispatch' actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in`call’  
journey (1.0.3) lib/journey/router.rb:68:in `block in call' journey (1.0.3) lib/journey/router.rb:56:in`each’  
journey (1.0.3) lib/journey/router.rb:56:in `call' actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in`call’  
actionpack (3.2.1)  
lib/action\_dispatch/middleware/best\_standards\_support.rb:17:in `call' rack (1.4.1) lib/rack/etag.rb:23:in`call’  
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in`call’  
actionpack (3.2.1) lib/action\_dispatch/middleware/params\_parser.rb:21:in  
`call' actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in`call’  
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' rack (1.4.1) lib/rack/session/abstract/id.rb:200:in`call’  
actionpack (3.2.1) lib/action\_dispatch/middleware/cookies.rb:338:in  
`call' activerecord (3.2.1) lib/active_record/query_cache.rb:64:in`call’  
activerecord (3.2.1)  
lib/active\_record/connection\_adapters/abstract/connection\_pool.rb:443:in  
`call' actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in`block in call’  
activesupport (3.2.1) lib/active\_support/callbacks.rb:405:in  
`_run __609157476__ call __595802435__ callbacks' activesupport (3.2.1) lib/active_support/callbacks.rb:405:in`\_\_run\_callback’  
activesupport (3.2.1) lib/active\_support/callbacks.rb:385:in  
`_run_call_callbacks' activesupport (3.2.1) lib/active_support/callbacks.rb:81:in`run\_callbacks’  
actionpack (3.2.1) lib/action\_dispatch/middleware/callbacks.rb:27:in  
`call' actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in`call’  
actionpack (3.2.1) lib/action\_dispatch/middleware/remote\_ip.rb:31:in  
`call' actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in`call’  
actionpack (3.2.1)  
lib/action\_dispatch/middleware/show\_exceptions.rb:56:in `call' railties (3.2.1) lib/rails/rack/logger.rb:26:in`call\_app’  
railties (3.2.1) lib/rails/rack/logger.rb:16:in `call' actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in`call’  
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' rack (1.4.1) lib/rack/runtime.rb:17:in`call’  
activesupport (3.2.1)  
lib/active\_support/cache/strategy/local\_cache.rb:72:in `call' rack (1.4.1) lib/rack/lock.rb:15:in`call’  
actionpack (3.2.1) lib/action\_dispatch/middleware/static.rb:53:in `call' railties (3.2.1) lib/rails/engine.rb:479:in`call’  
railties (3.2.1) lib/rails/application.rb:220:in `call' rack (1.4.1) lib/rack/content_length.rb:14:in`call’  
railties (3.2.1) lib/rails/rack/log\_tailer.rb:14:in `call' rack (1.4.1) lib/rack/handler/webrick.rb:59:in`service’  
/home/administrator/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in  
`service' /home/administrator/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in`run’  
/home/administrator/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in  
`block in start\_thread’

- 
-

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 8, 2012, 2:27pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/2 "2012-04-08T14:27:27Z")

</div>

On 6 April 2012 20:25, Vikram A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For some reason (most likely in my view due to naming conventions) rails  
> association is not working. Trying to access child object from parent object  
> gives undefined method.
> 
> …

> validates :title, :presence =\> true  
> class QuestionResponse \< ActiveRecord::Base  
> :destroy]
> 
> @question = Question.find(params[:id])  
> def find\_question  
> Error with trace:
> 
> undefined method `question\_responses’ for nil:NilClass
> 
> app/controllers/questions\_controller.rb:55:in `find\_question\_response’

Read the error carefully, it says that nil has not got a method  
question\_responses, so the object you are trying to call the method on  
is nil. So apparently @question is nil, assuming that line 55 is  
@question\_response = @question.question\_responses.find(params[:id])

The reason is that this is being called in a before\_filter and you  
have not got any code there to setup @question.

Colin

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 7:27am UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/3 "2012-04-09T07:27:29Z")

</div>

Hi Colin,

The error here is not the issue, I have rectified it. My point is having  
declared the associations properly with belongs\_to, has\_many…when in  
the console I don’t get any association methods. I should be able to  
access child object from parent object based on these declarations, but  
continues to show ‘undefined method’.

Thanks!

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 8:33am UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/4 "2012-04-09T08:33:53Z")

</div>

On 9 April 2012 06:27, Vikram A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Colin,
> 
> The error here is not the issue, I have rectified it. My point is having  
> declared the associations properly with belongs\_to, has\_many…when in  
> the console I don’t get any association methods. I should be able to  
> access child object from parent object based on these declarations, but  
> continues to show ‘undefined method’.

You have not quoted the previous message so I have not idea what this  
message is about. Remember this is a mailing list not a forum.  
Please post the exact error you are seeing (copy/paste it here from  
the console if you are seeing it in the console) and post the relevant  
sections of code also (the association definitions if it seems to be a  
problem with them).

Colin

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 12:44pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/5 "2012-04-09T12:44:30Z")

</div>

Hi,

Try to get the Question object first. from that move to Question  
Response.

On Sun, Apr 8, 2012 at 5:55 PM, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > resources :questions do  
> > has\_many :question\_responses, :dependent =\> :destroy, :class\_name =\>  
> > belongs\_to :question, :foreign\_key =\> “question\_id”  
> > def index  
> > @question = Question.new(params[:question])  
> > @question.question\_responses.build(params[:question\_response])  
> > @question = Question.find(params[:id])  
> > flash[:notice] = “Question has been updated.”  
> > flash[:notice] = “Question has been deleted.”  
> > params[:question\_response][:question\_id])  
> > undefined method `question\_responses’ for nil:NilClass

–  
Thanks & Regards,  
MuraliDharaRao.T  
+91-9642234646

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 1:26pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/6 "2012-04-09T13:26:03Z")

</div>

You have wrong entry in database…apply condition if !@question.blank?  
then @question.question\_responses  
end

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 1:18pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/7 "2012-04-09T13:18:51Z")

</div>

Colin L. wrote in post #1055588:

> On 9 April 2012 06:27, Vikram A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi Colin,
> > 
> > The error here is not the issue, I have rectified it. My point is having  
> > declared the associations properly with belongs\_to, has\_many…when in  
> > the console I don’t get any association methods. I should be able to  
> > access child object from parent object based on these declarations, but  
> > continues to show ‘undefined method’.
> 
> You have not quoted the previous message so I have not idea what this  
> message is about. Remember this is a mailing list not a forum.  
> Please post the exact error you are seeing (copy/paste it here from  
> the console if you are seeing it in the console) and post the relevant  
> sections of code also (the association definitions if it seems to be a  
> problem with them).
> 
> Colin

Hi Colin,

I am sorry for not quoting the previous message. I posted this question  
on Ruby on Rails Talk Google Group, not sure how it ended up here.  
Anyhow, my previous message was in response to this post of yours:

> > For some reason (most likely in my view due to naming conventions) rails  
> > association is not working. Trying to access child object from parent object  
> > gives undefined method.
> > 
> > …

> > validates :title, :presence =\> true  
> > class QuestionResponse \< ActiveRecord::Base  
> > :destroy]
> > 
> > @question = Question.find(params[:id])  
> > def find\_question  
> > Error with trace:
> > 
> > undefined method `question\_responses’ for nil:NilClass
> > 
> > app/controllers/questions\_controller.rb:55:in `find\_question\_response’

> Read the error carefully, it says that nil has not got a method  
> question\_responses, so the object you are trying to call the method on  
> is nil. So apparently @question is nil, assuming that line 55 is  
> @question\_response = @question.question\_responses.find(params[:id])

> The reason is that this is being called in a before\_filter and you  
> have not got any code there to setup @question.

> Colin

Now I know from the book “Beginning Rails 3” that having belongs\_to and  
has\_many declarations provide the associated association methods. I have  
these declarations in place as well as the foreign\_key in  
the question\_responses table that maps to the primary key of questions  
table, and yet when I declare a Question object and a QuestionResponse  
object and try to access the QuestionResponse object from the Question  
object (which is very well demonstrated in the book), I get this error  
message:

NoMethodError: undefined method `qr' for #<Question:0xa89bdcc> from /home/administrator/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.1/lib/active_model/attribute_methods.rb:407:in `method\_missing’  
from  
/home/administrator/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active\_record/attribute\_methods.rb:126:  
in `method_missing' from (irb):3 from /home/administrator/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start’  
from  
/home/administrator/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in  
`start' from /home/administrator/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.1/lib/rails/commands.rb:41:in `\<top (required)\>’  
from script/rails:6:in `require' from script/rails:6:in `’

I can successfully declare q = Question.first and qr =  
QuestionResponse.first, but doing q.qr (as demonstrated in the book)  
gives the error mentioned above.

Thanks!

P.S. This is my original question:  
[http://www.ruby-forum.com/topic/4025611?reply\_to=1055588#1055501](http://www.ruby-forum.com/topic/4025611?reply_to=1055588#1055501).

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 1:32pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/8 "2012-04-09T13:32:52Z")

</div>

On 9 April 2012 12:18, Vikram A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> NoMethodError: undefined method `qr’ for #Question:0xa89bdcc  
> from

I did ask you to post the code showing the model relationships and the  
section of code or console activity that is failing, rather than a  
textual description of what you have done. What is this ‘qr’ method  
that you seem to be trying to use? If you have question has\_many  
question responses and you have a question object (my\_question for  
example) then to get the responses you should use  
my\_question.question\_responses  
Note that it is plural and will be effectively an array of responses

Colin

> 

/home/administrator/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in

> To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> To unsubscribe from this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
gplus.to/clanlaw

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 2:56pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/9 "2012-04-09T14:56:39Z")

</div>

On 9 April 2012 13:34, Vikram A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > example) then to get the responses you should use
> > 
> > > To unsubscribe from this group, send email to  
> > > Thanks Colin, your suggestion worked. I did mention that qr is  
> > > QuestionResponse object. According to the book (Beginning Rails 3) that  
> > > I have, having say q as Question object and qr as QuestionResponse, I  
> > > should be able to do q.qr as you suggested  
> > > my\_question.question\_responses. Not sure why that is not working in my  
> > > case. However, this tells me that association is working. Many thanks  
> > > again.

Read the book more carefully and I am sure you will find that it is  
the name of the association after “q.” not the actual object name  
(they could be the same so this may be the confusion).

Colin

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 2:34pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/10 "2012-04-09T14:34:45Z")

</div>

Colin L. wrote in post #1055607:

> On 9 April 2012 12:18, Vikram A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > NoMethodError: undefined method `qr’ for #Question:0xa89bdcc  
> > from
> 
> I did ask you to post the code showing the model relationships and the  
> section of code or console activity that is failing, rather than a  
> textual description of what you have done. What is this ‘qr’ method  
> that you seem to be trying to use? If you have question has\_many  
> question responses and you have a question object (my\_question for  
> example) then to get the responses you should use  
> my\_question.question\_responses  
> Note that it is plural and will be effectively an array of responses
> 
> Colin
> 
> >

/home/administrator/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in

> > To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> > To unsubscribe from this group, send email to  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> > For more options, visit this group at  
> > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
> 
> –  
> gplus.to/clanlaw

Thanks Colin, your suggestion worked. I did mention that qr is  
QuestionResponse object. According to the book (Beginning Rails 3) that  
I have, having say q as Question object and qr as QuestionResponse, I  
should be able to do q.qr as you suggested  
my\_question.question\_responses. Not sure why that is not working in my  
case. However, this tells me that association is working. Many thanks  
again.

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 5:18pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/11 "2012-04-09T17:18:47Z")

</div>

Murali Tirupati wrote in post #1055602:

> Hi,
> 
> Try to get the Question object first. from that move to Question  
> Response.
> 
> Thanks & Regards,  
> MuraliDharaRao.T  
> +91-9642234646

Thanks, surely that is the idea 🙂

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 5:50pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/12 "2012-04-09T17:50:23Z")

</div>

you have two model question.rb and question\_response.rb

in question model write has\_many :question\_responses and in  
question\_response model write belongs\_to :question

in question\_responses table add attribute question\_id

now for fetch record

@a = Question.find(1) (1 is id )  
@a.question\_responses (if question\_id =1 in question\_responses table)

it will work for u.

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 5:32pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/13 "2012-04-09T17:32:13Z")

</div>

vishal singh wrote in post #1055605:

> You have wrong entry in database…apply condition if !@question.blank?  
> then @question.question\_responses  
> end

I tried nil, blank, empty, but to no avail. Now stuck with this error  
message:

Couldn’t find QuestionResponse with id=2 [WHERE  
“question\_responses”.“question\_id” = 2]

Not sure why would it continues to look for question\_responses, even  
after putting the condition. Works fine, as expected, otherwise.

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 3:47pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/14 "2012-04-09T15:47:46Z")

</div>

Colin L. wrote in post #1055618:

> On 9 April 2012 13:34, Vikram A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > example) then to get the responses you should use
> > > 
> > > > To unsubscribe from this group, send email to  
> > > > Thanks Colin, your suggestion worked. I did mention that qr is  
> > > > QuestionResponse object. According to the book (Beginning Rails 3) that  
> > > > I have, having say q as Question object and qr as QuestionResponse, I  
> > > > should be able to do q.qr as you suggested  
> > > > my\_question.question\_responses. Not sure why that is not working in my  
> > > > case. However, this tells me that association is working. Many thanks  
> > > > again.
> 
> Read the book more carefully and I am sure you will find that it is  
> the name of the association after “q.” not the actual object name  
> (they could be the same so this may be the confusion).
> 
> Colin

Heh never noticed that the book uses the same variable name as the name  
of the model, that really spun me around for quite a while. Thanks!

---

<div class="post-metadata">

**Author:** ![vikram\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a183cd/32.png) [@vikram\_a](https://www.ruby-forum.com/u/vikram_a)\
**Post date:** [April 9, 2012, 6:09pm UTC](https://www.ruby-forum.com/t/rails-association-not-working-undefined-method-error-in-trying-to-access-child-object-from-parent/218682/15 "2012-04-09T18:09:27Z")

</div>

vishal singh wrote in post #1055650:

> you have two model question.rb and question\_response.rb
> 
> in question model write has\_many :question\_responses and in  
> question\_response model write belongs\_to :question
> 
> in question\_responses table add attribute question\_id
> 
> now for fetch record
> 
> @a = Question.find(1) (1 is id )  
> @a.question\_responses (if question\_id =1 in question\_responses table)
> 
> it will work for u.

Yes, the models are set appropriately for association, as you explained,  
including foreign key. I have posted the models and other relevant codes  
in my first post, if you want to take a look:  
[http://www.ruby-forum.com/topic/4025611?reply\_to=1055650#1055501](http://www.ruby-forum.com/topic/4025611?reply_to=1055650#1055501)

The app works if @question\_responses.question\_id match @question.id, not  
otherwise. Hence the need for condition I guess, which did not work.
