Reputation: 5619
I got an error in My rails application
Code looks like this:
oders_controller.rb
def payMovie
@order = OrderMovie.new
@user = User.find(session[:user_id])
@order.user = @user
@movie = Movie.find params[:id]
puts "sssssssssssss"
puts @movie.inspect
@order.price = @movie.movieprice
@order.currency = @movie.currency
@order.movie << @movie
if @order.save
flash[:notice] = t("flash.saved")
redirect_to :back
else
redirect_to :back
end
end
models/user.rb
class User < ActiveRecord::Base
has_many :comment
has_and_belongs_to_many :knowledgeprovider
has_and_belongs_to_many :channel
belongs_to :oder_movie
models/order_movie.rb
class OrderMovie < ActiveRecord::Base
has_one :user
has_one :movie
end
What could be the problem?
Thanks for your help
UPDATE
@order.inspect
<OrderMovie id: nil, price: nil, currency: nil, user_id: nil, created_at: nil, updated_at: nil, movie_id: nil>
@user.inspect
<User id: 3, firstname: "Felix", lastname: "Hohlweglersad"
Upvotes: 0
Views: 138
Reputation: 622
Your User model relations are not good, if you're trying to make has_many :through relation between users and movies with order_movies, your user must has_many :order_movies not belongs_to :order_movies. So this error tells you that you don't have order_movie_id foreign key in your users table because you defined wrong relation. So change:
belongs_to :order_movie
to
has_many :order_movies
In your User model.
Upvotes: 1
Reputation: 3998
Here is the solution.You have spelled wrong in belongs_to :order_movie statement
class User < ActiveRecord::Base
has_many :comment
has_and_belongs_to_many :knowledgeprovider
has_and_belongs_to_many :channel
belongs_to :order_movie
If you don't have order_movie foreign key then run this migratuons
add_column :users,:order_movie_id,:integer
add_foreign_key :users,:order_movies
Upvotes: 1