I have a little -seems simple code that has me knocking my head around the place.
Here is a user model:
class User < ActiveRecord::Base
has_one :api_account, inverse_of: :user, dependent: :destroy
# Other user-specific validations come after
Here is the associated api_account:
class ApiAccount < ACtiveRecord::Base
belongs_to :user, inverse_of: :api_account
validates :access_token, presence: true
validates :user, presence: true,
uniqueness: true
In the api_account controller, I have:
response = ### Some JSON response from a distant server
@api_account = @user.create_api_account(:access_token => response[:access_token])
The schema for the api_account looks like this:
create_table "api_accounts", force: true do |t|
t.text "access_token"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
add_index "api_account", ["user_id"], name: "index_api_accounts_on_user_id"
Whenever I run the code, I get the access token in the response variable. When I use the rails console to check the database, I see a saved instance with valid creation and update time, but access_token and user_id have nil values.
First off, I still don't understand why these two fields have nil values. And second, why are they saved (i.e. pass validation) with nil values.
Any help would be more than welcome. Though, I sense a face palm coming! :)