Cha*_*les 2 mongoid ruby-on-rails-3.1
是否可以在mongoid中进行单向引用?
我想做的事情如下:
class User
include Mongoid::Document
include Mongoid::Timestamps
has_many :blogs, :class_name => "Blog", :inverse_of => :editor
has_one :active_blog, :class_name => "Blog", :inverse_of => :active_users
end
Run Code Online (Sandbox Code Playgroud)
和博客模型:
class Blog
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :editor, :class_name => "User", :inverse_of => :blogs
end
Run Code Online (Sandbox Code Playgroud)
所以,基本上,我希望用户存储引用其当前正在编辑/发布到的博客的对象ID.我不需要博客了解活跃用户,只有相反的方式.
看起来这样做的规范方法是在User上使用'belongs_to',在Blog上使用'has_many'.这确实有效,但它并不理想,因为它并没有真正在语义上表达两个模型之间的关系.
我是Mongoid的新手,并没有找到更好的答案.有没有更好的方法来建立这种类型的关系?
万分感谢!
如果您甚至不想active_user
在博客端创建访问者,您可以:
class User
belongs_to :active_blog, :class_name => "Blog", :inverse_of => nil
end
Run Code Online (Sandbox Code Playgroud)
另一方面,has_many/has_one和belongs_to对我来说似乎完全没问题.它不会将user_ids存储在博客中,并且博客不需要了解活动用户,除非您决定应该并且从博客端开始使用访问者.
归档时间: |
|
查看次数: |
2851 次 |
最近记录: |