This is just a simple question. I was trying to create a new object in Rails by passing in parameters to the constructor. However, when I execute the code, I get
SQLite3::SQLException: no such column: awards.user_id: SELECT "awards".* FROM "awards" WHERE "awards"."user_id" = 1
which means the object isn’t being constructed properly. Should I be using create instead of new? That isn’t working either.
def refresh_awards(user)
new_awards = []
if (user.karma < 40 ) #test award
a = Award.new(:name => "Nobody Award", :description => "From Jonathan", :category => "Community", :value => 1337, :level => 0, :handle => "nobody_award")
user.awards.append(a)
new_awards.append(a)
end
new_awards.each do |a|
flash[:notice] = "You received the " + a.name + "!"
end
end
Have you add
has_many :awardsto theUsermodel? Have you addedbelongs_to :userto theAwardmodel? Have you added the columnuser_idto theAwardmodel (using a migration)? You’ll need to do these three things to be able to use theuser.awardsmethod you’re using. Read the Rails Guide on Associations for more detail.Also,
appendisn’t a Ruby method – the closest method would be<<. You would use it like this:But you could neaten this into one line of code using the
createmethod:EDIT: To create the
user_idcolumn in theAwardmodel, run the following code from terminal (while in your app’s directory):