I had previously named my model incorrect as "Companies". Instead of renaming it, I renamed all references, dropped the database and recreated everything by rerunning the migrations. I'm able to see the table recreated as "Company" from SQLite explorer but when I run Company.new through the irb, I get:
ActiveRecord::StatementInvalid: Could not find table 'companies'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/sqlite_adapter.rb:295:in `table_structure'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/sqlite_adapter.rb:186:in `columns'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecor开发者_开发问答d-3.0.7/lib/active_record/base.rb:680:in `columns'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/persistence.rb:284:in `attributes_from_column_definition'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/locking/optimistic.rb:62:in `attributes_from_column_definition'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1395:in `initialize'
from (irb):1:in `new'
from (irb):1
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /Users/emzy/rails/blah/.bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I'm not sure where the reference for 'companies' is coming from - I can't find any references within the project's directory. All the ymls, etc. look fine.
Controller:
class CompanyDirectoryController < ApplicationController
before_filter :require_admin, :only => [:new, :create, :edit]
def new
@company = Company.new
@company.companylinks.build
end
Model:
class Company < ActiveRecord::Base
validates_presence_of :fullname, :detailed_description, :product_info, :cat_tags, :email
validates_uniqueness_of :fullname
has_many :companylinks, :class_name => 'CompanyLinks',
:foreign_key => "pid",
:dependent => :destroy
has_attached_file :company_photo,
:styles => { :medium => "300x300>",
:thumb => "100x100>" }
attr_accessor :photo_file_name
attr_accessor :photo_content_type
attr_accessor :photo_file_size
attr_accessor :photo_updated_at
accepts_nested_attributes_for :companylinks,
:allow_destroy => true
end
Routes:
resources :company, :controller => :company_directory
Why is it still looking for companies
instead of Company
? Any help would be great.
The table name ActiveRecord is looking up for the model Company is companies. Rails uses inflections to pluralize the name of the model to form the name of the table to query in the database. If you want to rename your table or have to work with a legacy database, you can use set_table_name in your model to set the table name.
UPDATE: There are some other useful tips at the rails wiki.
精彩评论