加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails的麻烦有很多关系

发布时间:2020-12-17 02:26:43 所属栏目:百科 来源:网络整理
导读:我正在编写一个应用程序,用户既可以创建自己的页面供人们发帖,也可以关注用户创建的页面上的帖子.这就是我的模特关系目前的样子…… class User ActiveRecord::Basehas_many :pageshas_many :postshas_many :followingshas_many :pages,:through = :followin
我正在编写一个应用程序,用户既可以创建自己的页面供人们发帖,也可以关注用户创建的页面上的帖子.这就是我的模特关系目前的样子……

class User < ActiveRecord::Base

has_many :pages
has_many :posts
has_many :followings
has_many :pages,:through => :followings,:source => :user

class Page < ActiveRecord::Base

has_many :posts
belongs_to :user
has_many :followings
has_many :users,:through => :followings

class Following < ActiveRecord::Base

belongs_to :user
belongs_to :page

class Post < ActiveRecord::Base

belongs_to :page
belongs_to :user

当我尝试在关系中努力工作以创建给定用户正在关注的页面(和相应帖子)的主页时,就会出现问题(类似于Twitter登录时用户主页的工作方式 – 提供给您的页面您所关注页面中所有最新帖子的综合视图)…

当我尝试调用followings.pages时,我收到“找不到方法”错误.理想情况下,我希望能够以一种方式调用User.pages,以获取用户所关注的页面,而不是他们创建的页面.

我是一个编程和Rails newb,所以任何帮助将不胜感激!在发布这个问题之前,我试图尽可能多地浏览这个网站(以及大量的谷歌搜索),但没有什么比我的问题更具体……

解决方法

您已经两次定义了页面关联.更改您的User类,如下所示:

class User < ActiveRecord::Base
  has_many :pages
  has_many :posts
  has_many :followings
  has_many :followed_pages,:class_name => "Page",:source => :user
end

现在让我们测试一下这个关联:

user.pages # returns the pages created by the user
user.followed_pages # returns the pages followed by the user

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读