title: Rails: Virtual attributes and form values
body: <p>I have a Model Book with a virtual attribute for create a Editor from the Book form.
  The code looks like:</p>
  
  <pre><code>class Book &lt; ActiveRecord::Base
    has_many :book_under_tags
    has_many :tags, :through =&gt; :book_under_tags
    has_one  :editorial
    has_many :written_by
    has_many :authors, :through =&gt; :written_by
  
    def editorial_string
     self.editorial.name unless editorial.nil?
     ""
    end
    def editorial_string=(input)
      self.editorial = Editorial.find_or_create_by_name(input)
    end
  end
  </code></pre>
  
  <p>And the new form:</p>
  
  <pre><code>&lt;% form_for(@book,
              :html =&gt; { :multipart =&gt; true }) do |f| %&gt;
    &lt;%= f.error_messages %&gt;
  
  ...
    &lt;p&gt;
      &lt;%= f.label :editorial_string , "Editorial: " %&gt;&lt;br /&gt;
      &lt;%= f.text_field :editorial_string, :size =&gt; 30  %&gt; &lt;span class="eg"&gt;Ej. Sudamericana&lt;/span&gt;
    &lt;/p&gt;
   ...
  </code></pre>
  
  <p>With this, when the form data no pass the validations I lost the data submited in the editorial field when the form is redisplayed, and also a new Editor is created. How I can fix this two problems? I am pretty new in ruby and I can't find a solution.</p>
  
  <p>UPDATE my controller:</p>
  
  <pre><code>  def create
      @book = Book.new(params[:book])
      respond_to do |format|
        if @book.save
          flash[:notice] = 'Book was successfully created.'
          format.html { redirect_to(@book) }
          format.xml  { render :xml =&gt; @book, :status =&gt; :created, :location =&gt; @book }
        else
          format.html { render :action =&gt; "new" }
          format.xml  { render :xml =&gt; @book.errors, :status =&gt; :unprocessable_entity }
        end
      end
    end
  </code></pre>