Supersonic
Supersonic

Reputation: 430

Problems Using collection_select to store value in the database

I have two models:

Project.rb

class Project < ActiveRecord::Base
 belongs_to :customer
end

and Customer.rb

class Customer < ActiveRecord::Base
  has_many :projects
end

Inside the _form.html.erb I have:

<p>
    <label>Select Customer</label>
    <%= f.collection_select :customer_id, Customer.all, :id, :name, :include_blank => true %>
</p>

Which should Collect the Customers from the Customer model and display all the customers, finally it should assign the value to the customer_id which is in projects table.

Rite now the everything is passing when i check the log. When I select the first customer with value=1, it passes customer_id = "1" in my log but it doesn't get stored in the table. It shows customer_id = nil in the projects table.

Can someone help. Thanks :)

Upvotes: 0

Views: 239

Answers (1)

Santosh
Santosh

Reputation: 1261

Do check that you added customer_id in attr_accessible method like,

class Project
  attr_accessible :your_other_attributes, :customer_id
end

Upvotes: 3

Related Questions