无论如何只为只读文档设置created_at时间戳吗?
我目前有以下消息类
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import fetch from 'node-fetch';
import PropTypes from 'prop-types';
import { Map, TileLayer } from 'react-leaflet';
export default class PageBackground extends Component{
constructor(props) {
super(props);
this.getLocation=this.getLocation.bind(this);
this.state={
position:[2,2]
};
}
componentWillMount() {
this.getLocation();
}
getLocation () {
fetch('http://freegeoip.net/json/')
.then((res)=>res.json())
.then((objec)=>{this.setState({position:[objec.latitude,objec.longitude]});console.log(this.state.position)})
.catch((err)=>{console.log("didn't connect to App",err);this.setState({position:['10','8']});})
}
render(){
return (
<Map center={this.state.position} zoom={13} >
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'/>
</Map>
);
}
}
它工作正常,但对于每条消息,我都在使用updated_at字段浪费空间,而该字段将始终与created_at相同
答案 0 :(得分:4)
浏览this page的时间戳部分。
include Mongoid::Timestamps - created_at and updated_at.
include Mongoid::Timestamps::Created - created_at only.
include Mongoid::Timestamps::Updated - updated_at only.
你甚至可以有短名称
include Mongoid::Timestamps::Short - c_at and u_at.
include Mongoid::Timestamps::Created::Short - c_at only.
include Mongoid::Timestamps::Updated::Short - u_at only.
答案 1 :(得分:1)
包括Mongoid::Timestamps::Created
而不是Mongoid::Timestamps
。
class Message
include Mongoid::Document
include Mongoid::Timestamps::Created
field :text, type: String
belongs_to :user, foreign_key: :user_id
embedded_in :conversation
end