Rails 模型关系
各位开发者大家好! 最近我一直在玩 Rails 3.0,经过大量研究后我有点陷入困境。 我想知道什么方法或解决方案最适合我的情况(我还找不到答案)。 所以我想要实现的目标是简单而直接的。
我想做这样的事情:
class User < ActiveRecord::Base
has_many :feeds
has_many :casts, :through => :feeds
end
class Feed < ActiveRecord::Base
has_many :users
has_many :casts
end
class Cast < ActiveRecord::Base
belongs_to :feed
end
所以最后我需要像 User.first.feeds 这样的方法来获取所有用户的提要,并使用 User.first.casts 来通过他/她的提要获取所有用户的转换。拥有 Feed.first.casts 和 Feed.first.users 也很好。非常简单,对吧,但我也很难为我想要实现的目标创建迁移。
我知道上面的代码不起作用 - 我一直在玩它,所以这只是我想要实现的概念。
基本上我的问题是:我应该通过连接模型以某种方式完成还是使用范围?(您也可以提供一个代码片段)以及如何为此进行迁移?
谢谢,抱歉,我在网上找不到关于这个简单案例的更多信息。
编辑:用户和 Feed 上的 has_and_belongs_to_many 在我的情况下不起作用,因为它不允许我拥有 @user.casts,它只提供 @user.feeds 和 @feed.users
Hello fellow developers!
Recently I've been playing with Rails 3.0 and after quite a bit of research I'm kinda stuck.
I want to know what approach or solution is the best in my case(I couldn't find an answer yet).
So what I'm trying to achieve is simple and straight forward.
I want to do something like this:
class User < ActiveRecord::Base
has_many :feeds
has_many :casts, :through => :feeds
end
class Feed < ActiveRecord::Base
has_many :users
has_many :casts
end
class Cast < ActiveRecord::Base
belongs_to :feed
end
So at the end I need to have methods like User.first.feeds to get all the user's feeds and User.first.casts to get all the user's casts through his/her feeds. Also would be nice to have Feed.first.casts and Feed.first.users. Pretty simple, right, but I'm also having a hard time to create migrations for what I'm trying to achieve.
I know that the code above won't work - I've been playing with it so this is just the concept of what I'm trying to achieve.
Basically my questions are: should I do it through join model somehow or use scopes?(also could you give a code snippet) and how do I do migration for that?
Thanks, and sorry I couldn't find much information on the web regarding this simple case.
Edit: has_and_belongs_to_many on User and Feed won't work in my case because it won't allow me to have @user.casts, it gives only @user.feeds and @feed.users
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要的是用户和提要之间的多对多关系。
您需要在代码中使用类似的内容,以使用户和提要关系发挥作用。
您可以在 Rails 指南中阅读更多相关内容 - http://guides.rubyonrails。 org/association_basics.html#the-has_and_belongs_to_many-association
您可能还想查看使用has_many :through 带有一个中间模型(此处解释 http://guides.rubyonrails.org/association_basics.html#choosing- Between-has_many-through-and-has_and_belongs_to_many)如果您想存储用户的任何元数据 - Feed 关系记录。
编辑:我设法在 3.0 和 3.1 上获得类似的设置(使用 has_many :through)。
这是我的模型的内容。
这是我使用的迁移
What you want is a many to many relationship between User and Feed.
You'll want something like this in your code for the User and Feed relationship to work.
You can read more about this in the Rails Guides - http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association
You may also want to look at using has_many :through with an intermediate model for this (explained here http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many) if you'd like to store any meta data for a user-feed relationship record.
Edit: I managed to get a similar setup working on 3.0 as well as 3.1 (using has_many :through).
Here are the contents of my models.
and here are the migrations I used
在 http://railscasts.com/episodes/265-rails-3-1-概述 Ryan Bates 表示,rails 3.1 支持链式 has_many :through 调用。 3.0里不可能了
In http://railscasts.com/episodes/265-rails-3-1-overview Ryan Bates says that rails 3.1 has support for chained has_many :through calls. It's not possible in 3.0