On Jun 11, 1:41 am, dave <[email protected]> wrote: > I keep getting this exception "SQLite3::SQLException: no such > column: books.user_id: SELECT "books".* FROM "books" WHERE > ("books".user_id = 4)" ,which sounds like there is no user_id in books > table.So i just installed foreigner plugin and added " > t.integer :user_id, :null => false" and > "add_foreign_key(:books, :users)" in book migration and i ran "rake > db:migrate" but still it is giving me the same exception.I am using > Rails 3 in windows and devise to authenticate user.
If you edit a migration that has already been run, rails doesn't know that you want to run it again. Either revert the changes to the first migration and add a second migration that adds the new column, or run rake db:migrate:redo to tell rails to undo the latest migration and then run it again (this will drop your books table and recreate it - don't do this if you have data you want to keep). Fred > HOME VIEW > <p><%= link_to "Add new Book",:controller =>"book", :action => > 'new' %></p> > <% @books.each do |b| %> > <p><%= b.author%></p> > <p><%= b.title%></p> > <%end%> > HOME CONTROLLER > > class HomeController < ApplicationController > def index > @user = current_user > @user.books||=Book.new > @[email protected] > end > end > BOOK CONTROLLER > > class BookController < ApplicationController > def new > @books = Book.new > # redirect_to :controller=>"home" ,:action=>"index" > end > > def create > @books = Book.new(params[:book]) > if @books.save > render "home/index" > #redirect_to :controller=>"home" ,:action=>"index" > else > > render :action => 'new' > end > end > > CREATE TABLE/BOOK MIGRATION > > class CreateBooks < ActiveRecord::Migration > def self.up > create_table :books do |t| > t.text :title > t.text :author > t.integer :user_id, :null => false > t.timestamps > end > add_foreign_key(:books, :users) > end > > BOOK VIEW > > <h1>Book#new</h1> > > <%= form_for(:book) do |f| %> > <p><%= f.text_field :title %></p> > <p><%= f.text_field :author %></p> > <p><%= f.submit "Add book"%> > BOOK MODEL > > class Book < ActiveRecord::Base > belongs_to :user > end > USER MODEL > > class User < ActiveRecord::Base > has_many :books > # Include default devise modules. Others available are: > # :token_authenticatable, :lockable, :timeoutable and :activatable > devise :database_authenticatable, :registerable, > :recoverable, :rememberable, :trackable, :validatable > > # Setup accessible (or protected) attributes for your model > attr_accessible :email, :password, > :password_confirmation,:firstname,:lastname,:school,:major,:sex,:zipcode > > end > > ROUTE > Campus::Application.routes.draw do > get "book/index" > > get "book/edit" > > get "book/new" > > get "home/edit" > > devise_for :users > resources :book > root :to=> "home#index" > match '/book/new' =>"home#index" > end > > DATABASE SCHEMA > ActiveRecord::Schema.define(:version => 20110609055608) do > > create_table "books", :force => true do |t| > t.text "title" > t.text "author" > t.datetime "created_at" > t.datetime "updated_at" > end > > create_table "users", :force => true do |t| > t.string "email", :default => > "", :null => false > t.string "encrypted_password", :limit => 128, :default => > "", :null => false > t.string "password_salt", :default => > "", :null => false > t.string "reset_password_token" > t.string "remember_token" > t.datetime "remember_created_at" > t.integer "sign_in_count", :default => 0 > t.datetime "current_sign_in_at" > t.datetime "last_sign_in_at" > t.string "current_sign_in_ip" > t.string "last_sign_in_ip" > t.datetime "created_at" > t.datetime "updated_at" > t.string "firstname" > t.string "lastname" > t.text "school" > t.text "major" > t.string "sex" > t.integer "zipcode" > end > > add_index "users", ["email"], :name => > "index_users_on_email", :unique => true > add_index "users", ["reset_password_token"], :name => > "index_users_on_reset_password_token", :unique => true > > end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

