Rails 3设置belongs_to关联的参数
我有一个跟踪社交数据的 Rails 应用程序。用户将能够创建群组并通过页面的社交 ID 将页面(即 Facebook 粉丝页面)添加到他们的群组中。由于用户可能会以其他人的身份添加页面,因此我对其进行了设置,以便每个社交 ID 只有一个页面。我还有一个名为分类的数据透视表,它将页面链接到组和用户。
我的模型关系设置如下:
User
has_many :groups
Group
belongs_to :user
has_many :categorizations
has_many :pages, :through => :categorizations
Page
has_many :categorizations
has_many :groups, :through => :categorizations
Categorization
belongs_to :group
belongs_to :page
现在,当我创建一个新页面并保存时,它正在创建一个新的分类。我遇到的问题是在分类中我需要手动设置 user_id 。我已经尝试过:
@page.categorizations.user_id
但我收到未定义的方法 user_id
错误。我可能从错误的方向接近这个问题,但是我将如何通过页面对象设置分类的 user_id 呢? 另外,如果重要的话,我正在使用 devise 来处理我的用户管理工作。
任何帮助将不胜感激。
谢谢
I have a rails app that is tracking social data. The users are going to be able to create groups and add pages(ie. facebook fan pages) to their groups by the page's social id. Since users could potentially be adding the page as someone else, I have it set up so that there is only one page per social id. I also have a pivot table called Catgorizations that links of the pages to the groups and users.
My model relationships are set up as follows:
User
has_many :groups
Group
belongs_to :user
has_many :categorizations
has_many :pages, :through => :categorizations
Page
has_many :categorizations
has_many :groups, :through => :categorizations
Categorization
belongs_to :group
belongs_to :page
Now when I create a new page and it saves, it is creating a new categorization. The problem I'm running into is that in the Categorization I need to set the user_id manually. I've tried:
@page.categorizations.user_id
But I get an undefined method user_id
error. I may be approaching this from the wrong direction but how would I go about setting the user_id of a categorization through the page object?
Also if it matters, I'm using devise to handle my user management stuff.
Any help would be greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要做的就是访问“连锁”关系组中几个深层的东西。
为了访问 User 模型的实例,给定一个页面,您需要获取其分类,选择一个(如何?),获取其组,然后查看该组的 user_id 是什么。
从概念上讲,一个页面实际上会有许多可能负责对其进行分类的用户。
为了让某事发生,您可以任意选择第一个分类,然后对其用户执行某些操作:
我不知道您所说的“设置”分类的用户 ID 是什么意思 - 它不拥有一个用户,所以我不知道您将如何处理该信息,抱歉!
What you're tring to do is to access something several levels deep in a 'chained' set of relationships.
In order to access an instance of the User model, given a page, you need to get its categorizations, pick one (how?), get its group, then see what the user_id is of that group.
Conceptually a page will actually have many users that might be in charge of categorizing it.
To get something to happen you could arbitrarily pick the first categorization and then do something with its user:
I don't know what you mean about 'setting' the user id for the categorization - it doesn't have a user, so I don't know what you'll then want to do with that information, sorry!
根据您的描述和模型,只有页面有用户,而不是分类,这就是为什么您收到它不存在的错误(它不在您的数据库中)。
另外,您在页面上缺少相反的关联:
这允许您从页面返回到用户:
From your description and your model, only Pages have a User, not Categorization, which is why you get the error that it doesn't exist (it's not in your database).
Also, you are missing the opposite association on Page:
This allows you to get back to User from Page: