Forum: Ruby on Rails activerecord.save attributes not saved

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
806f0051afc403ff24cb59b6763b4e90?d=identicon&s=25 Sergi Borras (Guest)
on 2005-12-12 15:57
(Received via mailing list)
Hi there,

I just added two new attributes, created (datetime) and user_id in a db
entity called documents. Those two new attributes are not being saved to
the
db by the activerecord model 'document', they remain null after @
document.save. I tried restarting the server, but the problem remains
the
same


Model :

class Document < ActiveRecord::Base
  has_many :tag_actions
  has_one :user
end


Controller create method

def create
    @document = Document.new
    @document.title = params[:document]['title']
    @document.body = params[:document]['body']
    @document.created = DateTime.now.strftime("Y-%m-%d %H:%M:%S")
    @document.user = User.find(session[:user_id])
    if @document.save
      flash[:notice] = 'Document was successfully created.'
      else
      flash[:notice] = 'Could not create document.'
    end
     redirect_to :action => 'list'
end
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2005-12-12 16:27
(Received via mailing list)
you should have:

class Document < ActiveRecord::Base
  belongs_to :user
  has_many :tag_actions
  ...
end

class User < ActiveRecord::Base
  has_many :documents
  ...
end

also, if you use the magic db field name 'created_on' of type datetime
(i'm
making the assumption you are using mysql), the datetime value for this
field will automatically be generated by AR upon creating the record.

def create
  @document = Document.new(@params[:document])
  @document.user_id = @session[:user_id]
  if @document.save
    flash[:notice] = "Document was successfully created"
    redirect_to :action => :list
  else
    flash.[:notice] = "Could not create document"
    render :action => :new
  end
end
This topic is locked and can not be replied to.