Hi,

I am new to RoR and trying to develop a simple web application.

I have created two tables namely users and products.

class CreateProducts < ActiveRecord::Migration
def self.up
create_table "products" do |t|
t.column "id", :int
t.column "name", :string
t.column "stock", :int
t.column "price", :float
t.column "user_id", :int
end
end

def self.down
drop_table "products"
end
end
------------------------------------------------------------
class CreateUsers < ActiveRecord::Migration
def self.up
create_table "users" do |t|
t.column "id", :int
t.column "name", :string
t.column "balance", :float
end
end

def self.down
drop_table "users"
end
end
-------------------------------------------------------------

And the relationship between these two tables are shown below:

class Product < ActiveRecord::Base
has_many :users
end

class User < ActiveRecord::Base
belongs_to roduct
end
-----------------------------------------------------------------

However, when I tried to display data from the users table to a droplist, I encountered error as shown below:

NoMethodError in Product#index
Showing app/views/product/index.rhtml where line #25 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.user_id

Extracted source (around line #25):

22: <br> <select name="product[user_id]">
23: <% @users.each do |user| %>
24: <option value="<%= user.id %>"
25: <%= ' selected' if user.id == @product.user_id %>>
26: <%= user.name %>
27: </option>
28: <% end %>

----------------------------------------------------------------

Can anyone tell me what's wrong with my code?

Thanks.

Regards,
Fung