多对多数据库
这可能是基本的东西,但我确实做了功课来查找它。我仍然找不到解决方案。
我有一个用户数据库、一个电影数据库和一个评级数据库。评级有 movie_id、user_id 和等级。
class Rating < ActiveRecord::Base
attr_accessible :grade
belongs_to :user
belongs_to :movie
end
class User < ActiveRecord::Base
has_many :ratings, :dependent => :destroy
has_many :movies, :through => :ratings
...
class Movie < ActiveRecord::Base
has_many :ratings, :dependent => :destroy
has_many :users, :through => :ratings
...
但我无法同时使用 movie_id 和 user_id 创建评级
rails c test
Loading test environment (Rails 3.0.3)
@movie=Factory(:movie)
#<Movie id: 1, title: "Rocky 20", link: "#", created_at: "2011-01-22 21:04:42", updated_at: "2011-01-22 21:04:42">
@user=Factory(:user)
#<User id: 1, name: "lola", email: "[email protected]", created_at: "2011-01-22 21:04:48", updated_at: "2011-01-22 21:04:48", encrypted_password: "c306a696138fa08c543ada3a3b4fd92067e9941743b7558e891...", salt: "f82c6abaccec17e2866d50150ad200181eb4bc8e0204249f171...", admin: false>
>> @user.ratings.create(:grade=> 4, :movie_id =>@movie.id)
=> #<Rating id: 1, grade: 4, user_id: nil, movie_id: nil, created_at: "2011-01-22 21:04:55", updated_at: "2011-01-22 21:04:55">
,“movie_id:nil”就是杀死我的原因......
那么当然我的测试没有通过: @user. rating.create(grade => 5, :movie_id => @movie.id) 没有 movie_id @movie. rating.create(grade => 5, :user_id => @user.id) 没有 user_id 有
任何提示吗?
谢谢!!
This is probably basic stuff, but I really made my homework looking it up. I still can´t find the solution.
I have a users db, a movies db, and a ratings db. Ratings has a movie_id, user_id and grade.
class Rating < ActiveRecord::Base
attr_accessible :grade
belongs_to :user
belongs_to :movie
end
class User < ActiveRecord::Base
has_many :ratings, :dependent => :destroy
has_many :movies, :through => :ratings
...
class Movie < ActiveRecord::Base
has_many :ratings, :dependent => :destroy
has_many :users, :through => :ratings
...
But I can´t create a rating with both movie_id and user_id
rails c test
Loading test environment (Rails 3.0.3)
@movie=Factory(:movie)
#<Movie id: 1, title: "Rocky 20", link: "#", created_at: "2011-01-22 21:04:42", updated_at: "2011-01-22 21:04:42">
@user=Factory(:user)
#<User id: 1, name: "lola", email: "[email protected]", created_at: "2011-01-22 21:04:48", updated_at: "2011-01-22 21:04:48", encrypted_password: "c306a696138fa08c543ada3a3b4fd92067e9941743b7558e891...", salt: "f82c6abaccec17e2866d50150ad200181eb4bc8e0204249f171...", admin: false>
>> @user.ratings.create(:grade=> 4, :movie_id =>@movie.id)
=> #<Rating id: 1, grade: 4, user_id: nil, movie_id: nil, created_at: "2011-01-22 21:04:55", updated_at: "2011-01-22 21:04:55">
that "movie_id: nil" is what is killing me...
then of course me tests are not passing:
@user.rating.create(grade => 5, :movie_id => @movie.id) has no movie_id
@movie.rating.create(grade => 5, :user_id => @user.id) has no user_id
any hints?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,您在此处显示的代码绝对可以工作。这让我猜测这是你没有在这里展示的东西。我会尝试以下几件事:
我已经尝试过您的示例,这就是我得到的:
您也可以使用像正如您可能知道的那样:
The code you're showing here should absolutely work IMO. Which leaves me guessing that it's something you aren't showing here. A couple of things I'd try:
I've tried your example and here's what I got:
Which you can also use like this, as you might know:
尝试像这样定义您的工厂:
然后要测试评级,请单独使用评级工厂:
Try defining your factories like this:
Then to test a rating, use the rating factory by itself: