问题
I can't delete information about tables and columns from ActiveRecord's cache. I'm using ActiveRecord for Ruby without Rails.
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:database => #
:password => #
)
class Person < ActiveRecord::Base
belongs_to :peoples
end
enter code here
class Persons < ActiveRecord::Base
belongs_to :peoples
end
class People < ActiveRecord::Base
has_many :persons
end
Person.new
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'vkusno.people' doesn't exist: SHOW FULL FIELDS FROM `people`'
Persons.new
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'vkusno.persons' doesn't exist: SHOW FULL FIELDS FROM `persons`'
But I try to connect to a Database without some table and columns.
Before I had a table in the database, "People, Peoples, Person, Persons", but I drop all my tables and restarted my server a few times.
If I change my database to sqlite, I get some don't exist tables, which it I'm working and drop that tables too.
How I can repair it?
UPD
ActiveRecord::Base.logger = Logger.new(STDOUT)
class AddFirst < ActiveRecord::Migration
def up
create_table :persons do |t|
t.integer :idd
t.string :name
t.string :href
t.string :sex
t.string :country
t.string :city
t.boolean :can_message
t.boolean :can_wall
t.string :photo
t.boolean :is_friend
t.boolean :is_client
end
create_table :people do |x|
x.integer :id_general
x.string :description
end
end
def down
drop_table :persons
drop_table :people
end
def keys
add_column :persons, :peoples_id, :integer
add_index :persons, :peoples_id
end
end
> AddFirst.new.up
-- create_table(:persons)
CREATE TABLE `persons` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `idd` int(11), `name` varchar(255), `href` varchar(255), `sex` varchar(255), `country` varchar(255), `city` varchar(255), `can_message` tinyint(1), `can_wall` tinyint(1), `photo` varchar(255), `is_friend` tinyint(1), `is_client` tinyint(1)) ENGINE=InnoDB
-- create_table(:people)
CREATE TABLE `people` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `id_general` int(11), `description` varchar(255)) ENGINE=InnoDB
=> {}
AddFirst.new.keys
-- add_column(:persons, :peoples_id, :integer)
ALTER TABLE `persons` ADD `peoples_id` int(11)
-- add_index(:persons, :peoples_id)
CREATE INDEX `index_persons_on_peoples_id` ON `persons` (`peoples_id`)
=> nil
> Person.new
=> #<Person id: nil, id_general: nil, description: nil>
> Persons.new
=> #<Persons id: nil, idd: nil, name: nil, href: nil, sex: nil, country: nil, city: nil, can_message: nil, can_wall: nil, photo: nil, is_friend: nil, is_client: nil>
> People.new
=> #<People id: nil, id_general: nil, description: nil>
回答1:
Understanding how class names are mapped to table names
You need to understand how ActiveRecord decides the table name for a model.
If you have a class Person, the default table name will be people. AR does this by calling the tableize method on the class name, like this:
'Person'.tableize # => "people"
or
Person.name.tableize # => "people"
That means, for the Person class, you should create the people table (unless you want to override the default). Make sure you get the spelling correct; peoples will not work.
That also means you should not have another class People because the default table name will also be people:
'People'.tableize #=> "people"
There will be a clash, or at best, you'll confuse yourself.
It's not a good idea to use persons as the table name, unless you're overriding the default. That's because AR will not generate persons as the table name for class Person.
I strongly encourage you to read and understand the ActiveRecord basics guide before you try anything else.
Modeling the real world
I'm also not sure why you want Person to belong to :peoples.
Can you describe the real-world scenario you're trying to represent?
Examples:
- a person has many books
- a person belongs to an organization.
来源:https://stackoverflow.com/questions/17506860/bug-with-activerecords