0

EDIT3: To wit, DO NOT put migration code in your model.rb files!!!

EDIT2: THE QUESTION (?) : does ANY migration code belong in a model.rb file?

EDIT: Just mentioning extra (system/config/etc) information that I need to share in order to get a good answer to this question from someone (even if it's not you) would be greatly appreciated. (1-ups for good tips on stack overflow optimization strategies)

First of all, here is the command prompt activity:

C:\Users\davo\Desktop\RailsProjects\simple_cms>rails c
Loading development environment (Rails 3.2.3)
irb(main):001:0> subject = Subject.find(1)
←[1m←[36mSubject Load (1.0ms)←[0m  ←[1mSELECT `subjects`.* FROM `subjects` WHERE       `subjects`.`id` = 1
LIMIT 1←[0m
=> #<Subject id: 1, name: "Initial Subject", position: 1, visible: true, created_at:"2012-05-18 01:00:26", updated_at: "2012-05-18 01:11:21">
irb(main):002:0> subject.pages
(Object doesn't support #inspect)

The basic schema is that we have two models here, page.rb and subject.rb. Subject is the parent of Page, as you will see. Here are the two models.

Guide to viewing this code: I think all that is relevant to this problem in these two models are the has_many and belongs_to tags. And I admit, I feel like there should be some foreign keys here. Should there be foreign keys here? Or is that wrong too?

subject.rb

class Subject < ActiveRecord::Base
  # attr_accessible :title, :body
  has_many :pages

  scope :visible, where(:visible => true)
  scope :invisible, where(:visible => false)
  scope :search, lambda {|query| where(["name LIKE ?", "%#{query}%"])}
end

page.rb

class Page < ActiveRecord::Base
  has_many :sections
  belongs_to :subject
  # attr_accessible :title, :body
  create_table "Pages" do |t|
  t.string "name"
  t.string "permalink"
  t.integer "position"
  t.boolean "visible?"

  end
end

I'm really new at this, so please forgive me if I didn't give you some piece of information that you need. Please let let know what extra information you need, I'm not sure where the error is coming from but I know this is a model (M....VC) issue. 95% on that one.

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100
  • 1
    What you have here so far looks ok, other than the errant `end` in page.rb. – x1a4 May 18 '12 at 06:04
  • I noticed that too earlier. But assumed that the create_table method needed that "end". Trying the changes now. – boulder_ruby May 18 '12 at 06:09
  • EDIT: RubyMine shows an error for not adding an end for the create_table method. The do |t| needs an end. – boulder_ruby May 18 '12 at 06:10
  • But wait...you're not supposed to do migrations in a model.rb file are you? Hmmmm checking things. – boulder_ruby May 18 '12 at 06:12
  • oh, i see. i didn't realize because the inner block wasn't indented, yes the `create_table` needs the `end`. You don't do migrations in models, though. You generate a migration with `rails g migration migration_name`, then put the code in the generated file. – x1a4 May 18 '12 at 06:15
  • 2
    for the love of God, don't put migrations in a model. I don't know what resources you are using to learn Rails, but maybe try this: http://ruby.railstutorial.org/ – Andrew Kuklewicz May 18 '12 at 06:22
  • Agree with @AndrewKuklewicz on ruby.railstutorial.org, if you're willing to part with a few shekels http://pragprog.com/book/rails4/agile-web-development-with-rails is a good book and https://peepcode.com/products/meet-rails-3-i is excellent screencast (plus you retain more than through book) – kreek May 18 '12 at 06:29

1 Answers1

3

You have a migration in your model.

create_table "Pages" do |t|
  t.string "name"
  t.string "permalink"
  t.integer "position"
  t.boolean "visible?"
end

Should be in ./db/migrate/{timestamp}_create_pages.rb. This file was generated for you if you did rails g model page

You also need a subject_id column to store the relation to subject

class CreatePages < ActiveRecord::Migration
  def change
    create_table :pages do |t|
      t.integer :subject_id
      t.string :name
      t.string :permalink
      t.integer :position
      t.boolean :visible?

      t.timestamps
    end
  end
end
kreek
  • 8,774
  • 8
  • 44
  • 69