Frage

So I'm trying to get the updated at attribute for my database object, but it's not working. I can get the other attributes but not the updated_at or created_at attributes. I'm also noticing that I can't get the id attribute as well. It seems as if anything I haven't declared in my Pocket.new does not exist. Here's my code:

By the way, I'm not using rails. I'm using sinatra-activerecord and sinatra.

Model

class Pocket < ActiveRecord::Base     
    def retrieve(consumer_key) 
        url = "get/"
        pocket_url = join_url(url)
        # time = Time.now.to_i
        token = self.token
        puts consumer_key
        puts self 
        puts time = self.updated_at  #.to_i this is where it happens.
        options = {
            :access_token => token,
            :consumer_key => consumer_key, 
            :since => (time if defined? time)
        }

        hello = RestClient.post pocket_url, options
        # JSON.parse(hello)
    end

Controller

get '/auth/pocket/callback' do
      […]

      pocket = Pocket.new(token: @token, user: @user)

DB migration

class CreatePocket < ActiveRecord::Migration
  def up
    create_table :pockets do |t|
        t.string :user
        t.string :token
        t.timestamps    
    end 
  end

  def down
    drop_table :users
  end
end
War es hilfreich?

Lösung

id, created_at, and updated_at are going to be nil for un-persisted objects, which is what you have by just calling .new and not saving the object yet.

Those attributes will be sent as soon as you call .save (assuming there are no validation errors).

Actually that isn't 100% true. updated_at will not be set on the first create / save, but it will be set on subsequent saves.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top