これはおそらく基本的なものですが、実際に調べて宿題を作りました。私はまだ解決策を見つけることができません。
ユーザーデータベース、映画データベース、評価データベースがあります。評価には、movie_id、user_id、および 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
...
しかし、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: "lola@gmail.com", 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) has no movie_id @movie.rating.create(grade => 5, :user_id => @ user.id) には user_id がありません
ヒントはありますか?
ありがとう!!