Почему мой user_id равен нулю?

def destroy
  @dignity.destroy
end

Извините, это не код, это то, что я чувствую сейчас. Я знаю, что на Devise есть масса вопросов для начинающих, думаю, я просмотрел почти каждый из них.

У меня очень простая установка Devise в Rails 3. Я сделал:

rails generate devise User

Я также использую подключаемый модуль rails 3 GeoKit (не уверен, что это актуально, просто знайте, что у меня есть эта другая модель), так что у меня есть другая модель, называемая Location, и она plays_as_mappable.

Прежде чем я отправлю код, основная проблема заключается в том, что я не могу заставить user_id автоматически увеличиваться. Я так понимаю, что немного магии Rails должно позаботиться об этом за меня, если я добавлю столбец с именем user_id в класс Location. (что я сделал при миграции.), а затем просто установите has_many и own_to соответственно. (см. ниже)

Я не могу понять, почему user_id всегда равен нулю. Это как-то связано с тем, как работает движок Devise? Я почти уверен, что раньше я использовал этот тип ассоциации точно так же, когда не использовал 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

вот миграция, которая добавил столбец:

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

  def self.down
    remove_column :locations, :user_id
  end
end

И, наконец, вот schema.rb:

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

РЕДАКТИРОВАТЬ: Я не против RTFM-ответа, если я могу получить небольшой толчок в правильном направлении. У меня есть подозрение, что мне нужно сообщить rails что-то в действии create моего location_controller.rb? Кто-нибудь, просто дайте мне небольшую подсказку!

6
задан Kevin 16 February 2011 в 02:39
поделиться