0
我目前正在關注Ryan Bates教程activity feed from scratch。保存originator_id,活動源從零開始
**我向數據庫添加了一個originator_id,以便我可以保存發起該帖子的所有者的ID。但由於某種原因,我無法讓它工作。
我從頭
class CreateActivities < ActiveRecord::Migration
def change
create_table :activities do |t|
t.belongs_to :user
t.string :action
t.belongs_to :trackable
t.string :trackable_type
###I want to save the id corresponding to User who created the object
t.belongs_to :originator
t.string :originator_type
t.timestamps
end
add_index :activities, :user_id
add_index :activities, :trackable_id
add_index :activities, :originator_id
end
end
這裏數據庫是我的代碼
模型
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :trackable, polymorphic: true
belongs_to : originator, polymorphic: true
attr_accessible :action, :recipient, :trackable
###how can i set the originator_id value
after_create :set_originator
def set_originator
self.originator.update_attribute(:originator, ???)
end
end
控制器
class ApplicationController < ActionController::Base
###sets the action and trackable values
###how can i the originator here. i keep getting an error saying undefined method
###why is it that rails recognizes trackable?
def track_activity(trackable, action = params[:action])
current_user.activities.create! action: action, trackable: trackable,
originator: originator
end
end
class LikesController < ApplicationController
def create
@like = Like.create(params[:like])
@dailypost = @like.dailypost
###Used to call track activity method above
track_activity @like
respond_to do |format|
format.js
format.html { redirect_to :back }
end
end
末