ruby-on-rails – 模型可以属于多个模型吗?
发布时间:2020-12-17 03:09:46 所属栏目:百科 来源:网络整理
导读:class Comment ActiveRecord::Base belongs_to :post belongs_to :user end 因此,通过上述关联,我可以从给定的评论对象中获取用户和帖子详细信息吗? 喜欢 @comment.post.post_title and @comment.user.user_name. 另请注意,我已将评论用作帖子的嵌套资源. r
class Comment < ActiveRecord::Base belongs_to :post belongs_to :user end 因此,通过上述关联,我可以从给定的评论对象中获取用户和帖子详细信息吗? @comment.post.post_title and @comment.user.user_name. 另请注意,我已将评论用作帖子的嵌套资源. resources :posts do resources :comments end 解决方法
是的,您可以,并且您不需要指定外键或类名来执行此操作. Saying belongs_to:user表示rails将在comments表中查找user_id整数字段,并期望存在名为User的ActiveRecord类.
添加任意多个,它们不会相互干扰. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |