나는 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
나는 그들은 일반적으로 사용하고 같은 레일에 belongs_to
has_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