mongoid

Extract `Moped::BSON::Document` attributes in Ruby hash

泪湿孤枕 提交于 2019-12-22 09:31:32
问题 In Mongoid 3.0.21, how to get all model's attributes as a plain Ruby Hash ? Calling either #attributes or #raw_attributes returns Moped::BSON::Document . While it actually extends Hash , several hash method does not work as expected. Particularly #except returns unmodified self , not hash with given keys stripped off . Update : Moped::BSON::Document properly inherits behavior of Hash . I was trying to name attributes with symbols, not strings, that's why #except didn't work. Shortly: do

Compare string ID to BSON::ObjectId

冷暖自知 提交于 2019-12-22 08:04:48
问题 I have an array of made up of type BSON::ObjectId and I want it to compare against some IDs as strings. if my_array_of_BSON_ObjectIds.include?(@my_id_as_a_string) # delete the item from the array else # add the item to the array as a BSON::ObjectId end This is not working as the types are different, can I turn my string into a BSON::ObjectId ? If so, how? 回答1: You can use BSON::ObjectId(@my_id_as_a_string) for representation your id as BSON::ObjectId refs http://api.mongodb.org/ruby/current

how to make has_and_belongs_to_many relationship work in mongoid

我与影子孤独终老i 提交于 2019-12-22 07:39:31
问题 i have the following code in the rails company model: class Company include Mongoid::Document include Mongoid::Timestamps field :name, type: String ... has_and_belongs_to_many :users end User model: class User include Mongoid::Document include Mongoid::Timestamps include ActiveModel::SecurePassword field :email, type: String ... has_and_belongs_to_many :companies end There is a company record in the database, and a user record and they are associated. For some reason, the following code does

Grouping Mongoid Objects by Day

青春壹個敷衍的年華 提交于 2019-12-22 07:11:08
问题 After much playing around in the console, I came up with this method to group activerecord-like (Mongoid) objects by the day on which they occured. I'm not sure this is the best way to accomplish this, but it works. Does anyone have a better suggestion, or is this a good way to do it? #events is an array of activerecord-like objects that include a time attribute events.map{ |event| # convert events array into an array of hashes with the day of the month and the event { :number => event.time

Using a combination of ANDs and ORs in Mongoid

余生颓废 提交于 2019-12-22 07:01:13
问题 I'd like to construct a query of the form: select * from some_table where (field1 = 'x' or field2 = 'y') and (field3 = 'z' or field4 = 'w') From reading the docs, I thought it should look something like this in Mongoid : SomeTable.or({:field1 => 'x'}, {:field2 => 'y'}) .and # or is it .intersect? .or({:field3 => 'z'}, {:field4 => 'w'}) But then that doesn't work - the mongo selector is simply an "$or" of all the fields. What's the correct way to do this? thanks. I'd also appreciate the

In a MongoDB query, what does a negative limit mean?

旧城冷巷雨未停 提交于 2019-12-22 01:50:34
问题 I'm using the Mongoid ruby gem to interact w/ MongoDB and when I try to get one of something from a query, it adds $limit: -1 (i.e. negative one) when I would expect it to just use 1 . I tried doing the same thing in the console and it didn't change which document was returned. Does a negative limit mean something special? 回答1: There is a note on negative limits under the "numberToReturn" heading in the "OP_QUERY" section of the "Mongo Wire Protocol" documentation. "If the client driver

update_attributes returns always true, even if nested_attributes are not valid

时光怂恿深爱的人放手 提交于 2019-12-21 20:06:56
问题 I have 2 models with nested data: class Goodtender include Mongoid::Document include Mongoid::Timestamps field :name field :count references_many(:offerprices, :autosave => true) accepts_nested_attributes_for :offerprices, :allow_destroy => true, :reject_if => :all_blank validates_presence_of :name, :message => "Invalid" validates_numericality_of :count, :message => 'Invalid' validates_associated :offerprices, :message => 'Invalid' end class Offerprice include Mongoid::Document include

using elasticsearch to filter through tags with whitespace

家住魔仙堡 提交于 2019-12-21 06:06:52
问题 I am using tire (https://github.com/karmi/tire) with mongoid. Here is my model definition: class SomethingWithTag include Mongoid::Document include Mongoid::Timestamps field :tags_array, type: Array include Tire::Model::Search include Tire::Model::Callbacks mapping do indexes :tags_array, type: :array, index: :not_analyzed end end Say I have a document {tags_array: ["hello world"]}. Then the following queries work fine: SomethingWithTag.tire.search { filter :terms, :tags_array => ["hello"] }

Mongoid relational Polymorphic Association

自闭症网瘾萝莉.ら 提交于 2019-12-21 02:22:31
问题 Does anyone know how to do a polymorphic association in Mongoid that is of the relational favor but not the embedding one. For instance this is my Assignment model: class Assignment include Mongoid::Document include Mongoid::Timestamps field :user field :due_at, :type => Time referenced_in :assignable, :inverse_of => :assignment end that can have a polymorphic relationship with multiple models: class Project include Mongoid::Document include Mongoid::Timestamps field :name, :type => String

Heroku is not pre-compiling my assets to prepare my app for asset pipeline

无人久伴 提交于 2019-12-20 14:13:14
问题 I'm having a bit of trouble getting my asset pipeline setup working with Heroku/Cedar using Rails 3.1.1 I've pushed my application and it launches successfully but there was no message that said "Preparing asset pipeline" and no static assets are being served up. No JS, CSS, or images are able to be found as there is no slug compilation time or runtime asset compilation being done. Any help would be greatly appreciated. They describe what should happen during deployment at this link which I