0
這裏是我的模型message.rb:Rails的Mongoid時間戳似乎不工作
class Message
include Mongoid::Document
include Mongoid::Timestamps::Created
embedded_in :room
field :content
field :user_id # That's the guest_id; room_id if it's a room's owner message.
end
時間戳* created_at *應該由Mongoid自動創建,但目前還沒有這樣的屬性Message.created_at在數據庫。我不明白爲什麼。
的embeds_many模型:
class Room
include Mongoid::Document
include ActiveModel::SecurePassword
embeds_one :guest
embeds_many :messages
has_secure_password
field :password_digest
field :owner_name
field :url
end
這是我創建的房間:
def create
@room = Room.new(room_params)
@room.password_confirmation = @room.password # Easy way to bypass the has_secure_password confirmation.
@room.url = generate_url
respond_to do |format|
if @room.save
session[@room.id.to_s.to_sym] = true
format.html { redirect_to "/chat/" + @room.url, notice: 'Room was successfully created.' }
format.json { render action: 'show', status: :created, location: @room }
else
format.html { render action: 'new' }
format.json { render json: @room.errors, status: :unprocessable_entity }
end
end
end
這是我如何創建消息:
def write_message
@room = Room.find(params[:id])
@room.messages.build(content: params[:message], user_id: params[:user_id])
if @room.save
render json: true
else
render json: @room.errors, status: :unprocessable_entity
end
end
我使用Rails 4.0.0和Mongoid 4.0.0.beta1。
@muistooshort我與它進行編輯。 – fschuindt
你如何創建對象? –
@artmees使用對象創建方法進行編輯。 – fschuindt