Note that there are some explanatory texts on larger screens.

plurals
  1. PORails, one-to-many relationship error in rails console
    primarykey
    data
    text
    <p>I need to set up a one to many relationship between my "Teacher" model and my "Robot" model. Each teacher has many robots but each robot only has one teacher.</p> <p>I thought I had it set up properly but when I try to play around with it in rails console typing this:</p> <pre><code>teacher1.robots = [robot1, robot2] </code></pre> <p>where teacher1 is a valid teacher and robot1 and robot2 are both valid robots, yields this error:</p> <pre><code>SQLite3::SQLException: no such column: robots.teacher_id: SELECT "robots".* FROM "robots" WHERE "robots"."teacher_id" = 14 ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: robots.teacher_id: SELECT "robots".* FROM "robots" WHERE "robots"."teacher_id" = 14 </code></pre> <p>I am very new to Ruby (and Ruby on Rails) but in all the research I've done, I can't find any mistake in the way I've set up my models or migrations... Here are the two model files and the migration.</p> <p>*_create_robots.rb:</p> <pre><code>class CreateRobots &lt; ActiveRecord::Migration def up create_table 'robots' do |t| t.text 'serial' t.references 'teachers' end end def down ; drop_table 'robots' ; end end </code></pre> <p>robot.rb:</p> <pre><code>class Robot &lt; ActiveRecord::Base belongs_to :teacher end </code></pre> <p>teacher.rb:</p> <pre><code>class Teacher &lt; ActiveRecord::Base has_many :robots before_destroy :confirm_no_inventory protected def confirm_no_inventory unless self.usb_cords == 0 and self.simple_snaps == 0 and self.casters == 0 errors.add(:base, "All checked out items must be returned before #{self.name} can be deleted") return false end end end </code></pre> <p>Thank you in advance to anyone who took the time to look through this, any thoughts at all on what the problem is or how to fix it would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload