我目前正在从头开始关注有关活动提要的Ryan Bates 教程。
**我在数据库中添加了一个 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
结尾