【发布时间】:2016-12-23 01:19:01
【问题描述】:
以下是有关我当前问题的一些信息:
我有两个模型,用户和活动。用户可以创建活动并为它们添加书签。
我目前有一个允许用户创建活动的工作设置,我正在寻找修改它以便也能够为它们添加书签,但没有成功。
user.rb:
class User < ActiveRecord::Base
...
has_many :activities
...
end
活动.rb
class Activity < ActiveRecord::Base
...
belongs_to :user
...
end
我可以这样做吗?
class User < ActiveRecord::Base
has_many :activities
has_many :bookmarks, :through => :activities
end
具有“中间”模型的 many_to_many 是更合适的方法吗?
关于上述方法,当我尝试检索用户的书签时,这是我得到的:
ActiveRecord::HasManyThroughSourceAssociationNotFoundError (Could not find the source association(s) "bookmark" or :bookmarks in model Activity.
【问题讨论】:
-
你能描述更多吗?发生了什么,有什么错误信息吗?
-
是的,例如,当我使用上述方法解决问题并尝试检索用户的书签时,我收到此错误:“ActiveRecord::HasManyThroughSourceAssociationNotFoundError (Could not find the source association(s )“书签”或:模型活动中的书签。尝试“has_many :bookmarks, :through => :activities, :source =>
'。”谢谢您的回复 -
目前的schema怎么样,
Activity有很多bookmarks吗?
标签: ruby-on-rails database migration associations