1
私はこのポストモデルがあります:このattr_writer属性を実際のデータベース列に変換するにはどうすればよいですか?
class Post < ActiveRecord::Base
attr_accessible :title, :content, :tag_names
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
attr_writer :tag_names
after_save :assign_tags
before_create :init_sort_column
def tag_names
@tag_names || tags.map(&:name).join(" ")
end
private
def assign_tags
self.tags = []
return if @tag_names.blank?
@tag_names.split(" ").each do |name|
tag = Tag.find_or_create_by_name(name)
self.tags << tag unless tags.include?(tag)
end
end
def init_sort_column
self.content_changed_at = self.created_at || Time.now
end
end
とタグモデル:
class Tag < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :posts, :through => :taggings
has_many :subscriptions
has_many :subscribed_users, :source => :user, :through => :subscriptions
def tag_posts_count
"#{self.name} (#{self.posts.count})"
end
end
私はこれを行うことができますので、データベース内の実際の列にattr_writer :tag_names
を回すしたいと思います:Post.find_by_tag_names("drinks")
を。
どうすればこの問題を解決できますか?