需要一些有关 Rails 中模型关联的帮助
我的场景是我有一个用户模型、联系人模型和 profile_url 模型。
以下是用户和联系人模型之间的关联。
user has_many :contacts, :through=> :user_contacts
user has_many :user_contacts
contact has_many :users, :through=>:user_contacts
contact has_many :user_contacts
user_contact belongs_to :user
user_contact belongs_to :contact
到目前为止,一切都很好。当我尝试将这些模型与 profile_url 模型关联时,问题就出现了。这种情况是,每当用户在应用程序上注册时,都会为其分配一个配置文件 url,该配置文件 url 将是公共配置文件 url。因此,如果我注册了,我会得到类似 http://www.mysite.com/mike.fererra 作为我的个人资料的公共网址。现在,当我将您添加为我的联系人列表中的联系人时,您将被分配一个私有 URL 到我的个人资料(相同的个人资料,只是向您提供一个仅对您私有的唯一 URL)。所以基本上有两种方法或者更确切地说有两个网址来访问我的个人资料,一个公共网址和一个私人网址。很难解释为什么我需要分隔网址,但如果您想知道的话,就认为没有其他方法。
我看待这个问题的方式是通过 STI(单表继承),如下所示。
ProfileUrl < ActiveRecord::Base
PublicUrl < ProfileUrl
PrivateUrl < ProfileUrl
profile_url belongs_to :user
user has_one :profile_url
profile_url has_one :public_url
profile_url has_many :private_urls
private_url belongs_to :contact
contact has_one :private_url
我希望这是有道理的,简而言之,故事围绕用户的个人资料和访问该个人资料的方式展开(世界的公共网址和仅特定于该用户的联系人的私人网址,每个联系人都有一个唯一的私人网址联系人所属用户的相同配置文件)。配置文件表具有类型、user_id、contact_id、url 字段。我不知道这是否是处理这种情况的最佳方法,但如果您能在这种情况下帮助我,那就太好了。
My scenario is i have a user model, contact model and profile_url model.
Following is the association b\w user and contact models.
user has_many :contacts, :through=> :user_contacts
user has_many :user_contacts
contact has_many :users, :through=>:user_contacts
contact has_many :user_contacts
user_contact belongs_to :user
user_contact belongs_to :contact
so far so good. The problem comes when i try to associate these models with the profile_url model. The scenario is whenever a user gets registered on the application it will be assigned a profile url which will be a public profile url. So if i signed up i would have something like http://www.mysite.com/mike.fererra as a public url to my profile. Now when i add you as a contact in my contact list then you will be assigned a private url to my profile (same profile, just a unique url is given to you that is private to you only). So basically there are two ways or rather two urls to access my profile, a public url and a private url. It's kind of hard to explain why do i need to seperate the urls but just think that there's no other way if you're wondering.
The way i see this thing is through STI (single table inheritance) something like the following.
ProfileUrl < ActiveRecord::Base
PublicUrl < ProfileUrl
PrivateUrl < ProfileUrl
profile_url belongs_to :user
user has_one :profile_url
profile_url has_one :public_url
profile_url has_many :private_urls
private_url belongs_to :contact
contact has_one :private_url
i hope that makes sense, in a nutshell the story revolves around the profile of the user and ways to access that profile (public url for the world and private url specific to contacts of that user only, each contact will have a unique private url to the same profile of the user the contact belongs to). The profile table has a type, user_id, contact_id, url fields. I don't know if that's the best way to handle this situation but it would be great if you could help me in this situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要
user has_many :contacts, :through=> :user_contacts
在我看来,user_contacts
是存储用户联系人列表中每个联系人的私有 URL 的最佳位置。公共 url 可能应该存储在users
表中(每个用户一个)。As long as
user has_many :contacts, :through=> :user_contacts
it seems to me thatuser_contacts
is the best place to store private url for each contact in the user's contact list. And public url should probably be stored inusers
table (one per user).