Why is my user_id null?

def destroy @dignity.destroy end 

Sorry, this is not a code, this is how I feel right now. I know that there are a ton of newcomers to Devise, I think I looked at almost everyone.

I have a very simple Devise setup in Rails 3. I did:

rails are created by the developer

I also run the rails 3 GeoKit plugin (not sure if this is important, just know that I have this other model), so I have another model called Location and it acts_as_mappable.

Before posting the code, the main problem is that I cannot get user_id to increase automatically. I realized that a bit of Rails magic should take care of this for me if I add the user_id column to the Location class. (which I did with migration.) and then just set has_many and belongs accordingly. (see below)

I cannot understand why user_id is always null. Is this related to how the Devise engine works? I am sure that in the past I did similar associations work the same way when I did not use Devise.

user.rb:

 class User < ActiveRecord::Base has_many :locations # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, :lockable and :timeoutable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me end 

location.rb:

 class Location < ActiveRecord::Base belongs_to :user attr_accessible :street_adress, :city, :state, :zip, :item, :user_id acts_as_mappable :auto_geocode => true def address return "#{self.street_adress}, #{self.city}, #{self.state}, #{self.zip}, #{self.item}" end end 

here is the migration that added the column:

 class AddUseridToLocation < ActiveRecord::Migration def self.up add_column :locations, :user_id, :integer end def self.down remove_column :locations, :user_id end end 

And finally, here is the schema.rb schema:

 ActiveRecord::Schema.define(:version => 20110213035432) do create_table "locations", :force => true do |t| t.string "street_adress" t.string "city" t.string "state" t.string "zip" t.float "lat" t.float "lng" t.datetime "created_at" t.datetime "updated_at" t.string "item" t.integer "user_id" end create_table "users", :force => true do |t| t.string "email", :default => "", :null => false t.string "encrypted_password", :limit => 128, :default => "", :null => false t.string "password_salt", :default => "", :null => false t.string "reset_password_token" t.string "remember_token" t.datetime "remember_created_at" t.integer "sign_in_count", :default => 0 t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" end add_index "users", ["email"], :name => "index_users_on_email", :unique => true add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true end 

EDIT: I'm fine with the RTFM answer, so far I can push a little in the right direction. I have a suspicion that I need to say rails something in the create action of my_controller.rb? Someone just give me a little hint here!

+6
ruby-on-rails ruby-on-rails-3 devise has-many belongs-to
source share
1 answer
 def destroy @dignity.destroy end 

Clearly, the first thing to do is:

 raise self.esteem 

You say you cannot get user_id "auto-increment". I think you meant that user_id is not assigned (i.e., it is always zero). Can you show some of the code that assigns a location to the user? Any of these should work:

 @user.locations.build @user.locations << Location.new 

EDIT

To expand this a bit, let's say you have a query that looks like this:

 POST /users/37/locations 

And the submitted form contains input name=user[location][name] value="Paris" . A typical Rails controller that creates an action might look like this:

 def create @user = User.find(params[:user_id]) @user.locations.build(params[:user][:location]) if @user.save flash[:notice] = "Location created successfully" redirect_to user_locations_path(@user) else render :new end end 

β€œMagic” is basically Rails, inferring from the has_many statement that it needs to set the value of the foreign key column (β€œuser_id”) in the corresponding row in the location table. When you call @user.save , it adds a new line at locations and sets user_id to @user.id

+10
source share

All Articles