[英]Ruby on Rails: SQLite3::SQLException: no such column:
SELECT "groups".* FROM "groups"
INNER JOIN "groups_interests" ON "groups"."id" = "groups_interests"."group_id"
WHERE "groups_interests"."interest_id" = 1
SQLite3::SQLException: no such column: groups_interests.interest_id: SELECT "groups".* FROM "groups" INNER JOIN "groups_interests" ON "groups"."id" = "groups_interests"."group_id" WHERE "groups_interests"."interest_id" = 1
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: groups_interests.interest_id: SELECT "groups".* FROM "groups" INNER JOIN "groups_interests" ON "groups"."id" = "groups_interests"."group_id" WHERE "groups_interests"."interest_id" = 1
I think I have a misunderstanding about foreign keys and the has_many relation 我认为我对外键和has_many关系有误解
To get the error I used rails c 为了得到错误,我使用了rails c
Interest.find(1).groups
I also want this command to run correcty 我也希望此命令正确运行
Groups.find(5).interests
class Group < ActiveRecord::Base
attr_accessible :description, :name, :project_id
has_many :students
has_many :group_interests
has_many :interests, :through => :group_interests
belongs_to :project
validates :name, presence: true, uniqueness: { case_sensitive: false }
end
class Interest < ActiveRecord::Base
attr_accessible :name
has_many :group_interests
has_many :groups, :through => :group_interests
validates :name, presence: true, uniqueness: { case_sensitive: false }
end
class GroupInterest < ActiveRecord::Base
attr_accessible :group_id, :interest_id
belongs_to :groups
belongs_to :interests
end
I got the idea to do this from ruby on rails guides 我有想法从红宝石在导轨上做到这一点
The reason for your error: there are two typos 错误原因:有两种错别字
class GroupInterest < ActiveRecord::Base
attr_accessible :group_id, :interest_id
belongs_to :groups #should be :group
belongs_to :interests #should be :interest
end
Group
has_many :group_interests
(plural) Group
has_many :group_interests
(复数) GroupInterest
belongs_to :group
(singular) GroupInterest
:group
(单数) EDIT - Do not use has_and_belongs_to_many
unless you are sure that you will never need a new attribute in the association table. 编辑 -不要使用
has_and_belongs_to_many
除非您确定您永远不需要关联表中的新属性。 has_many :through
is much more flexible. has_many :through
更加灵活。
class GroupInterest < ActiveRecord::Base
attr_accessible :group_id, :interest_id
belongs_to :group
belongs_to :interest
end
Group.find(5).interests
Why don't you use has_and_belongs_to_many : 为什么不使用has_and_belongs_to_many :
class Group < ActiveRecord::Base
attr_accessible :description, :name, :project_id
has_many :students
has_and_belongs_to_many :interests
belongs_to :project
validates :name, presence: true, uniqueness: { case_sensitive: false }
end
class Interest < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :groups
validates :name, presence: true, uniqueness: { case_sensitive: false }
end
class GroupInterest < ActiveRecord::Base
attr_accessible :group_id, :interest_id
end
You need to change your table structure on join_table
. 您需要在
join_table
上更改表结构。 See the link provided for that. 请参阅为此提供的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.