我需要在使用MongoDB的应用程序中人性化一些属性Mongoid.似乎旧的做事方式就像
def self.human_attribute_name(attr, options = {})
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end
Run Code Online (Sandbox Code Playgroud)
但这已经被i18n方法所取代.在他们自己的文件中覆盖似乎是一种更清洁的方法.尽管如此,我可以说,这是一个产品ActiveRecord.使用时有没有办法实现同样的目的Mongoid?
我正在使用MongoDB,但我并没有真正使用它的动态字段功能
field :fb_followers => Integer
field :twitter_followers => Integer
field :twitter_rts => Integer
field :link_visiting => Integer
field :reduce_points_per_day => Integer
Run Code Online (Sandbox Code Playgroud)
我该如何写这个,所以每个字段对于模型都是可选的?
使用Ruby和Haml,我有一个成本的属性.我相信(我是红宝石的新手),它将是一个Float
目前,下面的行输出我的十进制格式,如4.5而不是4.50,这就是我想要的.
%span.value= "£#{event.beers.first.cost)}"
Run Code Online (Sandbox Code Playgroud)
这是我的啤酒类文件.
class Beer
include Mongoid::Document
embeds_many :ratings
field :name, type: String
field :country, type: Country
field :cost, type: Float
field :photos, type: PhotoArray, default: PhotoArray.new
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试优化mongodb查询.我有一个指标from_account_id,to_account_id和created_at.但是以下查询执行完整集合扫描.
{
"ts": {
"$date": "2012-03-18T20:29:27.038Z"
},
"op": "query",
"ns": "heroku_app2281692.transactions",
"query": {
"$query": {
"$or": [
{
"from_account_id": {
"$oid": "4f55968921fcaf0001000005"
}
},
{
"to_account_id": {
"$oid": "4f55968921fcaf0001000005"
}
}
]
},
"$orderby": {
"created_at": -1
}
},
"ntoreturn": 25,
"nscanned": 2643718,
"responseLength": 20,
"millis": 10499,
"client": "10.64.141.77",
"user": "heroku_app2281692"
}
Run Code Online (Sandbox Code Playgroud)
如果我不做or,只查询from_account_id或to_account_id订单,它很快.
获得理想效果的最佳方法是什么?我应该像一个数组一样在一个字段中保留account_ids(来自和来自)或许还有更好的方法.谢谢!
我想update_attributes,而不是检查信息是否更改
您只需将此代码传递给rails console现有的rails + mongoid项目即可
class TestModel
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
end
test = TestModel.new({:name => "name 1"})
test.save()
=> true
test
=> created_at: 2012-11-14 13:48:26 UTC, updated_at: 2012-11-14 13:48:26 UTC
test.changed?
=> false
test.name_changed?
=> false
test.update_attributes({:name => "name 2"})
=> true
test.changed?
=> false
test.name_changed?
=> false
test
=> created_at: 2012-11-14 13:48:26 UTC, updated_at: 2012-11-14 13:49:23 UTC
Run Code Online (Sandbox Code Playgroud)
我做错了什么或这是一个错误?
这一定是一个简单的,但我被卡住了...所以我使用Rails#3和Mongoid,并希望动态构建依赖于传递参数的查询,然后执行find().就像是
def select_posts
query = :all # pseudo-code here
if (params.has_key?(:author))
query += where(:author => params[:author]) # this is pseudo-code again
end
if (params.has_key?(:post_date))
query += where(:create_date => params[:post_date]) # stay with me
end
@post_bodies = []
Post.find(query).each do |post| # last one
@post_bodies << post.body
end
respond_to do |format|
format.html
format.json { render :json => @post_bodies }
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个带验证的模型,如下所示:
class Order
include Mongoid::Document
field :first_name, type: String
field :last_name, type: String
validates_presence_of :first_name, :message => "Can't be empty"
validates_presence_of :last_name, :message => "Can't be empty"
end
Run Code Online (Sandbox Code Playgroud)
我描述模型rspec和思想机器人shoulda:
describe Order do
# validations
it { should validate_presence_of(:first_name) }
it { should presence_of(:last_name) }
end
Run Code Online (Sandbox Code Playgroud)
但是我失败了:
Failures:
1) Order
Failure/Error: it { should validate_presence_of(:first_name) }
Expected errors to include "can't be blank" when first_name is set to nil, got errors: ["first_name Can't be empty (nil)", …Run Code Online (Sandbox Code Playgroud) 我有这两个模型:
class Presentation
include Mongoid::Document
embeds_many :presentation_rows
end
class PresentationRow
include Mongoid::Document
embedded_in :presentation
end
Run Code Online (Sandbox Code Playgroud)
在我的presentation_rows_controller.rb中,我有以下几行代码:
def show
@presentation = Presentation.find(params[:id])
@presentation_rows = @presentation.presentation_rows
end
def destroy
...
...
end
Run Code Online (Sandbox Code Playgroud)
在我的presentation_rows/show.html.haml中,我有以下几行代码:
- @presentation_rows.each do |presentation_row|
= link_to "Delete", presentation_row, method: :delete
Run Code Online (Sandbox Code Playgroud)
我在destroy控制器动作中尝试了很多方法,但它们都指向了一个显而易见的事实,即我试图破坏嵌入式文档而不通过它的父级.但是现在我在我的视图文件中有了它的id的presentation_row,我不应该被允许销毁它.
带有空毁灭动作的错误消息,fyi:
Started DELETE "/en/presentation_rows/516af0a983c336708300000f" for 127.0.0.1 at 2013-04-14 20:08:47 +0200
Processing by PresentationRowsController#destroy as HTML
Parameters: {"authenticity_token"=>"KHGG2dsTseCl88okOKW9JAlHb+VaK2lKIxb0ptAIC7A=", "locale"=>"en", "id"=>"516af0a983c336708300000f"}
MOPED: 127.0.0.1:27017 QUERY database=shop_import_development collection=users selector={"$query"=>{"_id"=>"511a813a83c336a0ea000001"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 fields=nil (0.3152ms)
MOPED: 127.0.0.1:27017 QUERY database=shop_import_development collection=presentations selector={"_id"=>"516af0a983c336708300000f"} flags=[:slave_ok] …Run Code Online (Sandbox Code Playgroud) 我是MongoDB和Mongoid的新手,Mongoid 3.1.4用于我的Rails(3.2.13)应用程序.我在遗留代码中看到人们使用两者type: Moped::BSON::ObjectId和type: String某处的参考模型.
例
class Team
include Mongoid::Document
field :room_id, type: String
field :leader_id, type: Moped::BSON::ObjectId
...
end
Run Code Online (Sandbox Code Playgroud)
(使用git blame我知道由2个不同的人写的这两行)
我做了一个搜索,但没有找到问题的答案何时/为什么使用什么类型?String要么Moped::BSON::ObjectId
谢谢,
我想这将是简单,当我实现它,但是经过一番搜索,谷歌搜索和文档的扫描,我似乎无法找到答案.
我有一个字段是一个散列,其中键是id,值是其他id的数组.例如:
{"52ab84929938c7f966d4f116"=>["52ab84919938c7f966d4ee7d"],
"52ab84929938c7f966d4f117"=>["52ab84919938c7f966d4ee7d"],
"52ab84929938c7f966d4f0cc"=>["52ab84919938c7f966d4ee7d", "52ab84929938c7f966d4f13d"],
"52ab84929938c7f966d4f147"=>["52ab84919938c7f966d4ee7d"]}
Run Code Online (Sandbox Code Playgroud)
这可能更适合图形类型的数据库,但我不熟悉这项技术,并已经围绕Mongoid构建了我的应用程序的其余部分.对于另一个复杂的问题,这是我能提出的最简单的解决方案.
所以我的问题是如何搜索记录,例如id:52ab84929938c7f966d4f0cc作为哈希字段中的键?
我尝试像这样做数组样式并返回零结果:
Course.all_in(:skills_available => sk.id)
Run Code Online (Sandbox Code Playgroud)
而且我知道这不起作用,但如果它做到了会很好:
Course.where(:skills_available.key => sk.id)
Run Code Online (Sandbox Code Playgroud) mongoid ×10
mongodb ×5
ruby ×3
activerecord ×1
haml ×1
objectid ×1
relationship ×1
rspec ×1
search ×1
shoulda ×1