多对多数据库

发布于 2024-10-14 05:26:12 字数 1634 浏览 8 评论 0原文

这可能是基本的东西,但我确实做了功课来查找它。我仍然找不到解决方案。

我有一个用户数据库、一个电影数据库和一个评级数据库。评级有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捂风挽笑 2024-10-21 05:26:12

在我看来,您在此处显示的代码绝对可以工作。这让我猜测这是你没有在这里展示的东西。我会尝试以下几件事:

  • 使用常规控制台进行更改
  • 不要使用工厂(也许是它们?),而是创建实际条目

我已经尝试过您的示例,这就是我得到的:

> m = Movie.create :title => "Foo"
=> #<Movie id: 1, title: "Foo", created_at: …> 

> u = User.create :name => "Marc"
=> #<User id: 1, name: "Marc", created_at: …> 

> u.ratings.create :grade => 4, :movie_id => m.id
=> #<Rating id: 1, grade: 4, user_id: 1, movie_id: 1, created_at: …> 
> m.ratings.create :grade => 3, :user_id => u.id
=> #<Rating id: 2, grade: 3, user_id: 1, movie_id: 1, created_at: …> 

您也可以使用像正如您可能知道的那样:

> m.ratings.create :grade => 3, :user => u
=> #<Rating id: 3, grade: 3, user_id: 1, movie_id: 1, created_at: …> 

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:

  • Use the regular console for a change
  • Don't use Factories (maybe it's them?), create actual entries instead

I've tried your example and here's what I got:

> m = Movie.create :title => "Foo"
=> #<Movie id: 1, title: "Foo", created_at: …> 

> u = User.create :name => "Marc"
=> #<User id: 1, name: "Marc", created_at: …> 

> u.ratings.create :grade => 4, :movie_id => m.id
=> #<Rating id: 1, grade: 4, user_id: 1, movie_id: 1, created_at: …> 
> m.ratings.create :grade => 3, :user_id => u.id
=> #<Rating id: 2, grade: 3, user_id: 1, movie_id: 1, created_at: …> 

Which you can also use like this, as you might know:

> m.ratings.create :grade => 3, :user => u
=> #<Rating id: 3, grade: 3, user_id: 1, movie_id: 1, created_at: …> 
冰雪之触 2024-10-21 05:26:12

尝试像这样定义您的工厂:

Factory.define :movie, :class => Movie do |f|
  f.title "Rocky 20"
  f.link "#"
end

Factory.define :user, :class => User do |f|
  f.name "Lola"
  f.email "[email protected]"
end

Factory.define :rating, :class => Rating do |f|
  f.movie { |m| m.association :movie}
  f.user { |u| u.association :user}
  f.grade 4
end

然后要测试评级,请单独使用评级工厂:

it "creates a rating" do
  rating = Factory.create(:rating)
  rating.user.name.should == "Lola"
  rating.movie.title.should == "Rocky 20"
end

Try defining your factories like this:

Factory.define :movie, :class => Movie do |f|
  f.title "Rocky 20"
  f.link "#"
end

Factory.define :user, :class => User do |f|
  f.name "Lola"
  f.email "[email protected]"
end

Factory.define :rating, :class => Rating do |f|
  f.movie { |m| m.association :movie}
  f.user { |u| u.association :user}
  f.grade 4
end

Then to test a rating, use the rating factory by itself:

it "creates a rating" do
  rating = Factory.create(:rating)
  rating.user.name.should == "Lola"
  rating.movie.title.should == "Rocky 20"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文