0

テーブルとカラムに関する情報を ActiveRecord のキャッシュから削除できません。RailsなしでRuby用のActiveRecordを使用しています。

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`'

しかし、いくつかのテーブルと列なしでデータベースに接続しようとしています。

データベースに「人、人、人、人」というテーブルを作成する前に、すべてのテーブルを削除して、サーバーを数回再起動しました。

データベースをsqliteに変更すると、存在しないテーブルがいくつか得られますが、それは私が作業していて、そのテーブルも削除します。

どうすれば修理できますか?

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>
4

1 に答える 1

0

クラス名がテーブル名にどのようにマップされるかを理解する

ActiveRecordがモデルのテーブル名を決定する方法を理解する必要があります。

クラスがある場合Person、デフォルトのテーブル名は になりますpeopletableizeAR は、次のように、クラス名でメソッドを呼び出すことによってこれを行います。

'Person'.tableize  # => "people"

また

Person.name.tableize   # => "people"

つまり、Personクラスの場合、peopleテーブルを作成する必要があります (デフォルトをオーバーライドしたい場合を除く)。つづりが正しいことを確認してください。peoples動作しませ

これは、デフォルトのテーブル名も次のようになるため、別のクラスを持つべきではないことも意味します。Peoplepeople

'People'.tableize  #=> "people"

衝突が発生するか、せいぜい混乱するだけです。

personsデフォルトを上書きしない限り、テーブル名として使用することはお勧めできません。これは、 AR がpersonsclass のテーブル名として生成されないためPersonです。

他のことを試す前に、 ActiveRecord の基本ガイドを読んで理解することを強くお勧めします。

現実世界のモデリング

なぜあなたがPersonに所属したいのかもわかりません:peoples

あなたが表現しようとしている現実世界のシナリオを説明できますか?

例:

  • 人はたくさんの本を持っています
  • 人は組織に属します。
于 2013-07-08T15:53:40.700 に答える