当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - undefined method `last_comment_at=' for #<BlogPost:0x0000000adb9110>

问题描述:

I create new post:

 def create

params[:blog_post][:draft] = params[:draft].present?

@post = current_user.blog_posts.new(blog_post_params)

@post.save!

redirect_to @post, notice: (@post.draft? ? 'Черновик сохранен' : 'Пост опубликован')

rescue ActiveRecord::RecordInvalid

flash[:error] = ":("

render :new

end

When i create new post, displaying error:

undefined method `last_comment_at=' for #<BlogPost:0x0000000adb9110>

app/models/blog_post.rb:86:in `set_last_comment_at'

app/controllers/blog_posts_controller.rb:25:in `create'

config/initializers/flash_session_cookie_middleware.rb:19:in `call'

Model BlogPost:

class BlogPost < ActiveRecord::Base

# attr_accessible :subject, :body, :tag_list, :commentable_by, :visible_by, :attachments_attributes

include ActiveModel::ForbiddenAttributesProtection

belongs_to :user

has_many :comments, as: :owner, dependent: :destroy

has_many :attachments, as: :owner, dependent: :destroy

has_many :photos, through: :attachments, source: :asset, source_type: 'Photo'

has_many :videos, through: :attachments, source: :asset, source_type: 'Video'

belongs_to :article

has_many :blog_post_subscriptions, dependent: :destroy

has_many :subscribers, through: :blog_post_subscriptions, class_name: 'User', source: :user

has_one :poll, as: :owner, dependent: :destroy

has_many :poll_items, dependent: :destroy

accepts_nested_attributes_for :attachments, :allow_destroy => true

accepts_nested_attributes_for :poll, allow_destroy: true,

reject_if: proc { |attributes| attributes['question'].blank? }

validates :user, :subject, :presence => true

validates :body, presence: true, if: :body_required?

validates :body, length: { maximum: 65000 }

validate :validate_duplicate, on: :create

def body_required?

!article.present?

end

# after_create :bonus_for_blog_post

# after_create :notify_subscribers

before_create :check_paid_attributes

after_create :set_last_comment_at

def notify_subscribers!

unless draft?

Resque.enqueue BlogPostNotifier, self.id

end

end

after_save :set_published_at

def set_published_at

if published_at.nil? and !draft?

update_column :published_at, Time.now

bonus_for_blog_post!

notify_subscribers!

end

end

define_index do

indexes subject

indexes body

indexes tags_line

indexes user.username, as: :blog_post_author

has created_at

has published_at

where "draft=0"

group_by 'subject, body, tags_line, blog_posts.published_at, users.username'

set_property delta: ThinkingSphinx::Deltas::ResqueDelta

end

def to_s

subject || "[без заголовка]"

end

scope :drafts, where(draft: true)

scope :public, lambda { where(draft: false).where(:published_at.lte => Time.now) }

scope :with_privacy, lambda { |u|

unless u.moderator?

friend_ids = u.friend_ids + [u.id]

public.where(' blog_posts.visible_by IS NULL OR visible_by = "all" OR ' +

'(blog_posts.visible_by = "me" AND user_id = ?) OR' +

'(blog_posts.visible_by = "friends" AND user_id IN (?)) OR ' +

'(blog_posts.visible_by = "fof" AND EXISTS ' +

'(SELECT id FROM friendships WHERE friendships.user_id = blog_posts.user_id AND ' +

'friendships.friend_id IN (?) LIMIT 1)) OR ' +

'(blog_posts.visible_by = "bl" AND NOT EXISTS ' +

'(SELECT id FROM black_list_items WHERE black_list_items.owner_type="User" AND black_list_items.owner_id=blog_posts.user_id AND black_list_items.blocked_user_id=?))', u.id, friend_ids, friend_ids, u.id)

end

}

def set_last_comment_at

self.last_comment_at = created_at

save

end

acts_as_taggable

scope :tagged, lambda {|tag| tagged_with(tag) if tag }

before_save :set_tags_line

def set_tags_line

self.tags_line = tag_list.join(', ')

end

def user_can_edit?(user)

self.user.id == user.id or user.moderator?

end

def user_can_comment?(u)

u.can_comment_blog_post? self

end

protected

def bonus_for_blog_post!

unless draft?

user.bonus(:blog_post_bonus)

end

end

def check_paid_attributes

unless user.paid?

self.commentable_by = self.visible_by = 'all'

end

end

def validate_duplicate

errors.add(:base, :duplicate) unless user.blog_posts.where(body: body, article_id: article_id, :created_at.gt => 1.minute.ago).empty?

end

private

after_update :expire_cache

def expire_cache

expire_fragment "#{dom_id}_body"

expire_fragment "#{dom_id}_body_short"

expire_fragment "#{dom_id}_attachments"

Rails.cache.delete "#{dom_id}_tags"

end

before_save :emoji

def emoji

self.body = Rumoji.encode self.body

end

end

I think, problem is here:

 def set_last_comment_at

self.last_comment_at = created_at

save

end

But why can't it find the method last_comment_at?

网友答案:

I fixed issue, i run migrate and added new column last_comment_at to table BlogPost

分享给朋友:
您可能感兴趣的文章:
随机阅读: