2012-04-23 12 views
0

私はfb_graph API宝石を見ていて、移行にいくつかのコードを持っていますhttps://github.com/nov/fb_graph_sample/blob/master/db/migrate/20110623075710_create_subscriptions.rb私は理解できません、つまり、列の型はbelongs_toです。列タイプ。レール:belongs_toテーブル上の列

class CreateSubscriptions < ActiveRecord::Migration 
    def self.up 
    create_table :subscriptions do |t| 
     t.belongs_to :facebook 
     t.string :object, :fields, :verify_token 
     t.text :history_json 
     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :subscriptions 
    end 
end 

は、私は、彼らが通常使用しているとのRailsでbelongs_tohas_many関連を理解し、彼らはタイプbelongs_toのカラムを必要としたことがないと私は、データベースが列のタイプを受け入れることを期待していません。

また、https://github.com/nov/fb_graph_sample/blob/master/app/models/subscription.rbのモデルではbelongs_to Facebookが宣言されていますが、実際にどのようにデータベース列が使用されているのかわかりません。誰でも説明できますか?

class Subscription < ActiveRecord::Base 
    belongs_to :facebook 

    validates :facebook, :object, :fields, :history_json, :verify_token, :presence => true 

    before_validation :setup, :on => :create 

    def history 
    JSON.parse(self.history_json) 
    end 

    def history=(history) 
    self.history_json = history.to_json 
    end 

    def subscribe!(callback) 
    Facebook.app.subscribe!(
     :object => self.object, 
     :fields => self.fields, 
     :callback_url => callback, 
     :verify_token => self.verify_token 
    ) 
    end 

    private 

    def setup 
    self.verify_token = ActiveSupport::SecureRandom.hex(16) 
    self.history = [] 
    end 

end 

ここFacebook.rbモデルへのリンクですhttps://github.com/nov/fb_graph_sample/blob/master/app/models/facebook.rb

Facebook.rb

class Facebook < ActiveRecord::Base 
    has_many :subscriptions 

    def profile 
    @profile ||= FbGraph::User.me(self.access_token).fetch 
    end 

    class << self 
    extend ActiveSupport::Memoizable 

    def config 
     @config ||= if ENV['fb_client_id'] && ENV['fb_client_secret'] && ENV['fb_scope'] && ENV['fb_canvas_url'] 
     { 
      :client_id  => ENV['fb_client_id'], 
      :client_secret => ENV['fb_client_secret'], 
      :scope   => ENV['fb_scope'], 
      :canvas_url => ENV['fb_canvas_url'] 
     } 
     else 
     YAML.load_file("#{Rails.root}/config/facebook.yml")[Rails.env].symbolize_keys 
     end 
    rescue Errno::ENOENT => e 
     raise StandardError.new("config/facebook.yml could not be loaded.") 
    end 

    def app 
     FbGraph::Application.new config[:client_id], :secret => config[:client_secret] 
    end 

    def auth(redirect_uri = nil) 
     FbGraph::Auth.new config[:client_id], config[:client_secret], :redirect_uri => redirect_uri 
    end 

    def identify(fb_user) 
     _fb_user_ = find_or_initialize_by_identifier(fb_user.identifier.try(:to_s)) 
     _fb_user_.access_token = fb_user.access_token.access_token 
     _fb_user_.save! 
     _fb_user_ 
    end 
    end 

end 

答えて

0

あなたの中に多くの質問一つの質問に約belongs_to 1がありますが、あなたは答えを見つけるだろうここに:t.belongs_to in migration

関連する問題