![](/img/trans.png)
[英]ruby on rails: multiple insertion SQLite3::SQLException: near “,”: syntax error:
[英]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
我認為我對外鍵和has_many關系有誤解
為了得到錯誤,我使用了rails c
Interest.find(1).groups
我也希望此命令正確運行
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
我有想法從紅寶石在導軌上做到這一點
錯誤原因:有兩種錯別字
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
(復數) GroupInterest
:group
(單數) 編輯 -不要使用has_and_belongs_to_many
除非您確定您永遠不需要關聯表中的新屬性。 has_many :through
更加靈活。
class GroupInterest < ActiveRecord::Base
attr_accessible :group_id, :interest_id
belongs_to :group
belongs_to :interest
end
Group.find(5).interests
為什么不使用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
您需要在join_table
上更改表結構。 請參閱為此提供的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.