Rails 哪里有 join,但返回父对象?
假设我有一个表示属于用户的电影列表的类:
class User < ActiveRecord::Base
has_one :movie_list
end
class MovieList < ActiveRecord::Base
belongs_to :user
has_many :movie_list_movie_relationships
has_many :movies, :through => :movie_list_movie_relationships
end
class Movie < ActiveRecord::Base
has_many :movie_list_movie_relationships
has_many :movie_lists, :through => :movie_list_movie_relationships
end
class MovieListMovieRelationship < ActiveRecord::Base
belongs_to :movie
belongs_to :movie_list
end
有没有办法在 MovieList 上有一个方法,该方法返回仅包含特定发行年份等电影的 MovieList。我想要类似的东西:
class MovieList < ActiveRecord::Base
....
def for_year year
movies.where(:year => year)
end
end
除了我希望结果本身是一个 MovieList ,而这返回一个电影数组。所以我可以这样做,例如:
list = User.first.movie_list.for_year(1999)
list.id # would give the id of the MovieList = User.first.movie_list.id
list.movies.first.id # would give the id of the first movie in list
我有一种感觉,我要么 a) 面临这个问题,因为我的设计很糟糕,如果有人能解释我应该如何设计这个,那就太好了,或b)要求一些相对平凡的东西,但我只是不知道正确的术语。
谢谢!
编辑
我有特定的原因想要一个 MovieList
而不是 Movie
数组。其一,我用 User has_one :movie_list
编写了问题,但实际上是 User has_many :movie_lists
(例如,“我在 DVD 上拥有的电影”、“我的电影想看”等)。这就是为什么我不只是在 User
和 Movie
之间使用简单的联接。我想要一个 MovieList
因为我希望传递列表的 id
以便轻松地将电影添加到特定列表;我有一个MovieListMovieRelationshipController。我认为我最终要做的就是将电影作为数组获取,然后手动传递列表 ID。
编辑 2
这就是我根据 @normalocity 和随后的评论最终所做的事情。如有任何进一步的意见/建议,我们将不胜感激:)
class MovieList
...
def limit_to_year year
new_list = MovieList.new(self.attributes)
new_list.id = self.id
new_list.movies = []
limited_movies = movies.where(:year => year)
new_list.movies = limited_movies
return new_list
end
end
Suppose I have a class that represents a list of movies, belonging to a user:
class User < ActiveRecord::Base
has_one :movie_list
end
class MovieList < ActiveRecord::Base
belongs_to :user
has_many :movie_list_movie_relationships
has_many :movies, :through => :movie_list_movie_relationships
end
class Movie < ActiveRecord::Base
has_many :movie_list_movie_relationships
has_many :movie_lists, :through => :movie_list_movie_relationships
end
class MovieListMovieRelationship < ActiveRecord::Base
belongs_to :movie
belongs_to :movie_list
end
Is there any way to have a method on MovieList that returns a MovieList containing only movies with, for example, a specific release year. I want something like:
class MovieList < ActiveRecord::Base
....
def for_year year
movies.where(:year => year)
end
end
EXCEPT that I want the result to be a MovieList itself, whereas this returns an array of Movies. So I could do, for example:
list = User.first.movie_list.for_year(1999)
list.id # would give the id of the MovieList = User.first.movie_list.id
list.movies.first.id # would give the id of the first movie in list
I have a feeling that I'm either a) faced with this problem because my design is bad, and it would be great if someone could explain how I should design this, or b) asking for something relatively mundane but I just don't know the right terminology.
Thanks!
Edit
I have specific reasons for wanting a MovieList
and not an array of Movie
s. For one, I've written the question with User has_one :movie_list
but in reality a User has_many :movie_lists
(e.g., "Movies I have on DVD", "Movies I want to see", etc). That's why I don't just use a simple join between User
and Movie
. I want a MovieList
because I want the id
of the list to get passed around to make it easy to add a movie to a specific list; I have a MovieListMovieRelationshipController
. I think what I'm going to end up having to do is fetch the movies as an array and just pass around the list id manually.
Edit 2
Here's what I ended up doing, as per @normalocity and the ensuing comments. Any further comments/suggestions are appreciated :)
class MovieList
...
def limit_to_year year
new_list = MovieList.new(self.attributes)
new_list.id = self.id
new_list.movies = []
limited_movies = movies.where(:year => year)
new_list.movies = limited_movies
return new_list
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能直接执行您希望的操作,不,但是创建一个空的
MovieList
很简单,然后从您的for_year
方法添加电影数组到新的MovieList
。您甚至可以通过named_scope
更快/更简单地查询关联的Movie
对象数组。在
Movie< /code> class:
现在,每次调用
Movie.for_year("1945")
时,您都会返回该年份的 Movie 对象数组。然后...
MovieList
(不属于任何用户),并将 #1 的结果添加到列表中这可能有效:
movies1945 = MovieList.create(:movies => Movie.for_year("1945")
如果它不起作用,您可能必须拆分新
MovieList
的创建code> 对象,以及将值分配给Movie
集合如果您需要区分简单的电影列表(我不知道它和数组有什么区别,但如果)。这种区别是必要的),以及特定用户在其集合中拥有的
Movies
,那么您可以将您的类分为MovieList
(只是电影列表)和 < code>Collection(给定用户拥有的电影)如果您不需要,那么 1945 年的电影列表可能有一个
nil
用户,或者(比如您的网站)。称为 imdb.com)您可以创建一个名为"imdb"
的虚拟用户帐户,并让该列表属于那个用户。You can't do what you're hoping for directly, no, but it's simple enough to create an empty
MovieList
, and then add the array of movies from yourfor_year
method to the newMovieList
. You can even make the query for the array of associatedMovie
objects faster/simpler via anamed_scope
.In
Movie
class:Now, every time you call
Movie.for_year("1945")
you get back an array of Movie objects from that year.Then...
MovieList
(that belongs to NO user), and add the results from #1 to the listThis might work:
movies1945 = MovieList.create(:movies => Movie.for_year("1945")
If it doesn't work, you might have to split up the creation of the new
MovieList
object, and the assignment of values to theMovie
collection.If you need to distinguish between a simple list of Movies (what's the difference between that and an Array, I don't know, but if that distinction is necessary), and the
Movies
that a particular user has in their collection, then you could separate your classes intoMovieList
(just a list of movies) and aCollection
(the movies that a given user has).If you don't need that, then the 1945 movie list could either have a
nil
user, or (say your web site is called imdb.com) you could create a dummy user account called"imdb"
, and have the list belong to that user.