# Newbie Question (belongs\_to and has\_many)

**URL:** <https://www.ruby-forum.com/t/newbie-question-belongs-to-and-has-many/110263>\
**Category:** Rails\
**Created:** [July 24, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/newbie-question-belongs-to-and-has-many/110263 "2007-07-24T19:28:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [July 24, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/newbie-question-belongs-to-and-has-many/110263/1 "2007-07-24T19:28:18Z")

</div>

Hi All,

I am running from the console. I have two models i\_test and  
test\_question. Model i\_test has\_many test\_questions and test\_questions  
belong\_to i\_test

Example 1 below works fine, but when I wrap the same code into a member  
function like in example 2, the test\_questions don’t save. (and I can’t  
figure out why). Thanks in advance for any help!

//////////////////////  
Example 1: (saves without a problem)

@t = ITest.new  
@q = TestQuestion.new  
@t.test\_questions \<\< @q  
@t.save

//////////////////////  
Example 2: (will not save the test\_questions)

@t = ITest.new  
@q = TestQuestion.new  
@t.add\_question(@q)  
@t.save

where add\_question is defined as the following:

class ITest \< ActiveRecord::Base  
has\_many :test\_questions

def add\_question(question)  
@test\_questions \<\< question  
end  
end

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [July 24, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/newbie-question-belongs-to-and-has-many/110263/2 "2007-07-24T19:59:47Z")

</div>

> where add\_question is defined as the following:
> 
> class ITest \< ActiveRecord::Base  
> has\_many :test\_questions
> 
> def add\_question(question)  
> @test\_questions \<\< question  
> end  
> end

Try changing the member function to

def add\_question(question)

> ```
> self.test_questions << question
> 
> ```
> 
> end

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [July 24, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/newbie-question-belongs-to-and-has-many/110263/3 "2007-07-24T20:05:30Z")

</div>

Mike H. wrote:

> This seems to work. I guess test\_questions is not an instance variable  
> in this case.

Exactly - this caught me out as well. It’s because an ActiveRecord  
object (which you extended your class out of) doesn’t store the data as  
instance variables - it makes a runtime connection to the database and  
just sort of sees what’s in there, and acts as a conduit to it’s  
corresponding table in the database (at least that’s how i understand  
it)

So you need to refer to the variables of a runtime object, hence ‘self’.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/newbie-question-belongs-to-and-has-many/110263/4 "2007-07-26T00:45:55Z")

</div>

> has\_many :test\_questions
> 
> def add\_question(question)  
> @test\_questions \<\< question  
> end

try this instead:

def add\_question(question)  
test\_questions \<\< question  
end

‘test\_questions’ is not an instance variable (words proceeded by @  
signs), it is a method added by has\_many.

Hope that helps,

Steve

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [July 24, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/newbie-question-belongs-to-and-has-many/110263/5 "2007-07-24T20:00:57Z")

</div>

This seems to work. I guess test\_questions is not an instance variable  
in this case.

class ITest \< ActiveRecord::Base  
has\_many :test\_questions

def add\_question(question)  
self.test\_questions \<\< question  
end  
end

Mike H. wrote:

> Hi All,
> 
> I am running from the console. I have two models i\_test and  
> test\_question. Model i\_test has\_many test\_questions and test\_questions  
> belong\_to i\_test
> 
> Example 1 below works fine, but when I wrap the same code into a member  
> function like in example 2, the test\_questions don’t save. (and I can’t  
> figure out why). Thanks in advance for any help!
> 
> //////////////////////  
> Example 1: (saves without a problem)
> 
> @t = ITest.new  
> @q = TestQuestion.new  
> @t.test\_questions \<\< @q  
> @t.save
> 
> //////////////////////  
> Example 2: (will not save the test\_questions)
> 
> @t = ITest.new  
> @q = TestQuestion.new  
> @t.add\_question(@q)  
> @t.save
> 
> where add\_question is defined as the following:
> 
> class ITest \< ActiveRecord::Base  
> has\_many :test\_questions
> 
> def add\_question(question)  
> @test\_questions \<\< question  
> end  
> end
