在Rails3中使用Sqlite3,我收到带有has_and_belongs_to_many情况的“ActiveRecord :: StatementInvalid:SQLite3 :: SQLException:no such column”。从连接到Sqlite3的rails控制台,page.editors或page.admin_users
时出错[[[编辑:由于连接表ID中的拼写错误而解决]]]
安装了宝石
rails 3.0.9
sqlite3 1.3.3 x86-mingw32
sqlite3-ruby
(win7 64bit开发机器)
page.rb - >行
> class Page > belongs_to :subject > has_many :sections > has_and_belongs_to_many :editors, :class_name => "AdminUser" > #has_and_belongs_to_many :admin_users > > end
admin_users.rb - >行
> class AdminUser > has_and_belongs_to_many :pages > scope :named, lambda {|first,last| where(:first_name => > first, :last_name => last)} > > end
迁移文件 - >发现错别字!
class CreateAdminUsersPagesJoin false do |t| t.integer :admin_users_id, :foreign_key => true # Should be admin_user_id t.integer :page_id, :foreign_key => true end add_index :admin_users_pages, [:admin_users_id, :page_id] # Again admin_user_id end def self.down drop_table :admin_users_pages end end
rails console错误
irb(main):004:0> page.admin_users ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: admin_users_pages.admin_user_id: SELECT * FROM "admin_users " INNER JOIN "admin_users_pages" ON "admin_users".id = "admin_users_pages".admin_user_id WHERE ("admin_users_pages".page_id = 2 )
感谢Heikki的帖子。我自己修好了,然后才回到这里(噢哦),但我很乐意接受答案。发布它,我会检查它,因为这是正确的。欢呼声。
答案 0 :(得分:3)
按照惯例,连接表中的外键应该是单数的。 admin_user_id
代替admin_users_id
。