groovehunter
groovehunter

Reputation: 3158

member variable in rails model not set as expected

I have a simple model entity, called "entry" with one field "content" and on save or update I want to process that content.

But in the models' method the member variable @content is nil, in the controller function it's still set.

entries_controller.rb

   def create
    @entry = Entry.new(params[:entry])

    respond_to do |format|

      if @entry.save
         #@entry.process
         @entry.splitwords

entry.rb

class Entry < ActiveRecord::Base

  def splitwords
    logger.debug "content: #{@content.inspect} "

    words = @content.split(" ")
    ...

and that's from the log

entry: #<Entry id: 6, content: "Air-Berlin-Vorstandschef Joachim Hunold legt sein A...", created_at: "2011-08-18 09:30:52", updated_at: "2011-08-18 09:30:52"> 
content: nil 

So why is @content not set inside an entry ??

Upvotes: 2

Views: 218

Answers (1)

wanderfalke
wanderfalke

Reputation: 878

Your problem is @content, which is indeed not defined in your model. What you want is to call "content" or "self.content" if you want. So your code would be:

  def splitwords
    logger.debug "content: #{content.inspect} "

    words = content.split(" ")
    ...

Upvotes: 2

Related Questions