Using Sqlite3 with Rails3 and I receive "ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column" with a has_and_belongs_to_many situation. From rails console connected to Sqlite3, errors when page.editors or page.admin_users
[[[EDIT: Solved because of typos in the join table id's]]]
gems installed
ra开发者_StackOverflow中文版ils 3.0.9
sqlite3 1.3.3 x86-mingw32
sqlite3-ruby
(win7 64bit dev machine)
page.rb --> OK
> 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 --> OK
> class AdminUser > has_and_belongs_to_many :pages > scope :named, lambda {|first,last| where(:first_name => > first, :last_name => last)} > > end
migration file --> Spot the Typos!
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 errors
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 )
Thanks to Heikki for the posts. I fixed it myself before looking back here only now (d'oh) but I'll happily accept the answer. Post it and I'll check it since that was correct. Cheers.
By convention the foreign keys in the join table should be singular ie. admin_user_id
instead of admin_users_id
.
- http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many
- http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association
精彩评论