Felix Felix - 6 months ago 11
Ruby Question

Rails unkown attribute

I got an error in My rails application

enter image description here

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"


enter image description here

Answer

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.