Forum: Ruby on Rails Newbie Question (belongs_to and has_many)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2953c93b1319b25c96c8cec6fc442d2d?d=identicon&s=25 Mike Halloran (foobarmike)
on 2007-07-24 19:28
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
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2007-07-24 19:59
> 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
2953c93b1319b25c96c8cec6fc442d2d?d=identicon&s=25 Mike Halloran (foobarmike)
on 2007-07-24 20:00
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 Halloran 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
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2007-07-24 20:05
Mike Halloran 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'.
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-07-26 00:45
(Received via mailing list)
>   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
This topic is locked and can not be replied to.