Topic: Model Association Question [Solved]

Hi, after going through many rails books I am a little lost.
For active model to work example

class parent
has_one :child

class child
belongs_to :parent

So if I specify parent_id in the child migration file and the child_id in the parent migration file.
If I do

@parent = Parent.find(1)

Then I do

@parent.child.create

It keeps saying child method not found. Shouldn't Rails knows that the child belongs to parent and create a child with the corresponding parent_id?

Last edited by limgw (2012-05-21 00:20:33)

Re: Model Association Question [Solved]

You don't need the child_id in the parent model's migration all that is needed is the parent_id in the child model's migration.
Look at: http://guides.rubyonrails.org/associati … ssociation

The when you are creating the child the correct syntax is:

@parent.create_child(params[:child])

Look at: http://guides.rubyonrails.org/associati … -reference

Re: Model Association Question [Solved]

As a correllary you can use build_child() to make an instance of child without saving it.

Re: Model Association Question [Solved]

I am getting error messages:
@student=Student.find(1)

@test=@student.test.create
irb(main):003:0> @test=@student.test.create()
NoMethodError: undefined method `scoped' for Test:Module
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/association.rb:124:in `target_scope'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/association.rb:88:in `scoped'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/singular_association.rb:42:in `find_target'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/association.rb:146:in `load_target'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/association.rb:56:in `reload'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/singular_association.rb:9:in `reader'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/builder/association.rb:41:in `test'
  from (irb):3
irb(main):008:0> @test = @student.create_test()
NoMethodError: undefined method `new' for Test:Module
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/reflection.rb:190:in `build_association'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/association.rb:236:in `build_record'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/singular_association.rb:55:in `create_record'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/singular_association.rb:21:in `create'
  from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.1/lib/active_record/associations/builder/singular_association.rb:37:in `create_test'
  from (irb):8

Re: Model Association Question [Solved]

I am having errors in my code and I can't figure out whats wrong please help.
3 models Student, Test and Question
I am using Devise Authentication
Question Migration File

class CreateQuestions < ActiveRecord::Migration
  def change
    create_table :questions do |t|
      t.integer :student_id
      t.integer :type
      t.integer :test_id    
      t.integer :questiondb_id
      t.string :topic
      t.integer :difficulty
      t.text :text
      t.boolean :multiresponse
      t.string :choice1
      t.string :choice2
      t.string :choice3
      t.string :choice4
      t.string :answer

      t.timestamps
    end
  end
end

Test Migration File

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.integer :student_id
      t.integer :total_questions
      t.integer :score
      t.boolean :completed
      t.datetime :time_limit

      t.timestamps
    end
  end
end

Question Model Class

class Question < ActiveRecord::Base
  belongs_to :test
  belongs_to :student

end

Student Class Model

class Student < ActiveRecord::Base
  has_one :test
  has_many :questions, :through =>:test
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
   :confirmable, :timeoutable
end

Test Class Model

class Test < ActiveRecord::Base
  belongs_to :student
attr_accessible :student_id, :question_id, :total_questions, :score, :completed, :time_limit,
end

From Rails console
When I execute s=Student.find(1)
It executes ok.
When I run any of
s.create_test()
s.build_test()
s.new_test()
It keeps returning no method error?!

Re: Model Association Question [Solved]

I think it is because you cannot have a model named test. Try something like this:

class Student < ActiveRecord::Base
  has_one :test, class_name: 'StudentTest'
  has_many :questions, :through =>:test
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
   :confirmable, :timeoutable
end

class StudentTest
  belongs_to :student
end

Re: Model Association Question [Solved]

Thanks I renamed my model and now it works.
But seriously this is a HEAD BANGER!
I literally spend more than a week googling to no avail about this, and it is not in the list of reserved words...