Ruby on rails 使用mongoid gem时出现未初始化常量错误
我已经开始使用mongoid gem,但是我得到了一个未初始化的常量错误 我已经定义了一个文档TweetRuby on rails 使用mongoid gem时出现未初始化常量错误,ruby-on-rails,ruby,mongodb,rake-task,Ruby On Rails,Ruby,Mongodb,Rake Task,我已经开始使用mongoid gem,但是我得到了一个未初始化的常量错误 我已经定义了一个文档Tweet class Tweet include Mongoid::Document field :tweet_id, type: Int field :created_at, type: DateTime field :text, type: String field :user_id, type: Int embedded_in :us
class Tweet
include Mongoid::Document
field :tweet_id, type: Int
field :created_at, type: DateTime
field :text, type: String
field :user_id, type: Int
embedded_in :user
end
另一个文档用户
class User
include Mongoid::Document
field :name, type: String
field :screen_name, type: String
field :user_id, type: Int
embeds_many :tweets
end
我想编写一个rake任务来创建tweets并将其插入数据库。
rake任务的代码
我将tweets存储在一个路径为@pathtofile的文件中
task :readtweet => :environment do
File.readlines(@pathtofile).each do |line|
line=line.chomp()
tweet_hash = JSON.parse(line)
Tweet.new(created_at: my_hash['created_at'], text: my_hash['text'] )
end
end
但每次我执行rake任务时,它都会给我错误
uninitialized constant Tweet::Int
/home/c0mrade/testapp/app/models/tweet.rb:4:in `<class:Tweet>'
/home/c0mrade/testapp/app/models/tweet.rb:2:in `<top (required)>'
/home/c0mrade/testsapp/lib/tasks/data.rake:22:in `block (3 levels) in <top (required)>'
未初始化的常量Tweet::Int
/home/c0mrade/testapp/app/models/tweet.rb:4:in`'
/home/c0mrade/testapp/app/models/tweet.rb:2:in`'
/home/c0mrade/testsapp/lib/tasks/data.rake:22:in'block(3层)in'
我已经按照上的安装说明进行了操作
有人能帮我解决这个错误吗?应该是
整数
而不是整数
?我不知道这是否能解决您的问题,但我在任何文档中都没有看到Int
(如果我错了,请纠正我)。谢谢您指出!我真蠢,竟然用Int。