When I rename a field in Mongoid, it successfully updates the field in MongoDB, but it does not update the field in the ruby code:
class Example
include Mongoid::Document
field :apple, type: String, as: :c_apple
end
Example.all
=> #<Mongoid::Criteria
selector: {}
options: {}
class: Example
embedded: false>
Example.all.rename("apple" => "d_apple")
MOPED: 127.0.0.1:27017 UPDATE database=core collection=examples selector={} update={"$rename"=>{"apple"=>"d_apple"}} flags=[:multi]
COMMAND database=core command={:getlasterror=>1, :w=>1} runtime: 1.2460ms
=> {"connectionId"=>2, "updatedExisting"=>false, "n"=>0, "syncMillis"=>0, "writtenTo"=>nil, "err"=>nil, "ok"=>1.0}
e = Example.new
=> #<Example _id: 5bfdbf103ed1492f9a000001, apple(c_apple): nil>
e.d_apple
NoMethodError: undefined method `d_apple' for #<Example _id: 5bfdbf103ed1492f9a000001, apple(c_apple): nil>
Why are the changes in MongoDB not reflected in class definition?
When you run #rename, you interact with MongoDB. The change of document structure is stored in the database. This method does not interact with ruby source code, you need to edit it by yourself.
Btw. I can't recall any ruby/rails method (besides ones in rake tasks and generators) that interacts with the source code stored in files.
Related
I'm using rails 4.2.5 with mongoid 5.1.0 on windows 10 in dev mode. I have created a model "Signup" but it won't save to MongoDB. I can see that rails connects to MongoDB but no transactions are performned.
What am I missing?
Model code:
class Signup
include Mongoid::Document
field :email, type: String
field :date, type: DateTime, default: ->{ Time.now }
end
Console tests:
irb(main):034:0> s = Signup.create
=> #<Signup _id: 57b9d0436fc5511c04a945ce, email: nil, date: 2016-08-21 16:01:07 UTC>
irb(main):035:0> Signup.count
=> 0
irb(main):036:0> s.save!
=> true
irb(main):037:0> Signup.count
=> 0
irb(main):038:0>
found the solution, I used a dot in the database name in mongoid.yml (project.io) which somehow hindered db creation. I removed it (project_io) and everything is working now as it should.
I have two Mongoid document classes in Rails with a N-N reference relationship, as below:
class Band
include Mongoid::Document
field :name, type: String
field :_id, type: String, default: ->{ name }
has_and_belongs_to_many :tags
end
class Tag
include Mongoid::Document
field :name, type: String
field :_id, type: String, default: ->{ name }
has_and_belongs_to_many :bands
end
Then I create new instances of each one and associate them:
2.0.0p247 :014 > band = Band.new(name: "beetles")
=> #<Band _id: beetles, name: "beetles", tag_ids: nil>
2.0.0p247 :015 > tag = Tag.new(name: "rock")
=> #<Tag _id: rock, name: "rock", band_ids: nil>
2.0.0p247 :016 > band.tags << tag
MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} (1.3745ms)
MOPED: 127.0.0.1:27017 UPDATE database=band_dev collection=bands selector={"_id"=>"beetles"} update={"$addToSet"=>{"tag_ids"=>{"$each"=>["rock"]}}} flags=[] (0.2341ms)
=> [#<Tag _id: rock, name: "rock", band_ids: ["beetles"]>]
Here a call is made to the database to update the driver class (in this case the "band"). The problem is that neither of these instances were saved and are brand new records!
2.0.0p247 :017 > band.new_record?
=> true
2.0.0p247 :018 > tag.new_record?
=> true
So why to perform a db roundtrip for an unsaved document? Is there a setting where I can turn this off?
Version Info:
mongoid (4.0.0 80ed36f)
moped (1.5.1)
origin (1.1.0)
bson (1.8.6)
bson_ext (1.8.6)
rails (4.0.0)
OS: Ubuntu 12.04 LTS
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
.new() method does not save it.
use .create() instead.
band=Band.new() does create a new object of Band class, but it does not persist.
When you push the relation using << method, the relation between those 2 new objects created.
So, when you query, those 2 objects are already in N-N relationship.
However, that is object living in this code execution , not really persisted as data.
To save it in your mongodb, you need to define the first object with .create() instead of new().
It may look like
band=Band.create(name:"beetles")
band.tags << Tag.new(name:"rock")
I am using sunspot to search my local db. After adding the gems, running the generate command, and booting up the solr server I do the following:
class Style < ActiveRecord::Base
attr_accessible :full_name, :brand_name
searchable do
text :full_name
text :brand_name
end
end
Added the above to my Style model and re-indexed (I had already indexed prior to creating this post, which is why I re-indexed to put it here)
funkdified#vizio ~/rails_projects/goodsounds.org $ rake sunspot:solr:reindex
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
*Note: the reindex task will remove your current indexes and start from scratch.
If you have a large dataset, reindexing can take a very long time, possibly weeks.
This is not encouraged if you have anywhere near or over 1 million rows.
Are you sure you want to drop your indexes and completely reindex? (y/n)
y
[#######################################] [14/14] [100.00%] [00:00] [00:00] [53.19/s]
Then I try a search and get nothing
1.9.3p392 :003 > Style.search { fulltext 'Monkey' }.results
SOLR Request (10.4ms) [ path=#<RSolr::Client:0x0000000685ab28> parameters={data: fq=type%3AStyle&q=Monkey&fl=%2A+score&qf=full_name_text+brand_name_text&defType=dismax&start=0&rows=30, method: post, params: {:wt=>:ruby}, query: wt=ruby, headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, path: select, uri: http://localhost:8982/solr/select?wt=ruby, open_timeout: , read_timeout: , retry_503: , retry_after_limit: } ]
=> []
But, wait shouldn't it have worked and picked this up?
Style.first
Style Load (1.3ms) SELECT "styles".* FROM "styles" LIMIT 1
=> #<Style id: 54, brand_name: "Monkey", full_name "Monkey Chicken", created_at: "2013-02-01 23:25:58", updated_at: "2013-02-16 03:02:16">
Here is one more clue. I am seeing "unknown field" for brand_name (setup in Style.rb)
If you change the schema (the "searchable" block) you have to either reindex all models:
rake sunspot:solr:reindex
or reindex that specific model with a given batch size (here 500):
rake sunspot:solr:reindex[500,Style]
as per the Sunspot doco on Github (search "Reindexing Objects").
FYI, to use Style.reindex for non-schema changes, you will have to call Sunspot.commit to save changes.
In app/services, I have some classes, as Notification::Finder and Notification::Builder.
They are placed as app/services/notification/builder.rb and app/services/notification/finder.rb.
There is also the Notification class as a model, at app/models/notification.rb
The autoload_path is configurated as in config.autoload_paths += %W(#{config.root}/app/services)
When I try to load Finder, it works:
Loading development environment (Rails 3.2.9)
[1] pry(main)> Notification::Finder
=> Notification::Finder
But when I try the Builder, I get a problem with the rails autoloading:
Loading development environment (Rails 3.2.9)
[1] pry(main)> Notification::Builder
=> ActiveRecord::Associations::Builder
It just ignores the namespace I've used when the constant name (Builder) has already been defined by other namespace, and gets the ActiveRecord::Associations::Builder instead.
Is this the expected behavior, or a rails bug?
Going more detailed, the const_missing method at activesupport/dependencies.rb receives a const_name 'Builder', and nesting.inspect => 'nil'.
Curious that when I use constantize, it resolves as expected:
Loading development environment (Rails 3.2.9)
[1] pry(main)> 'Notification::Builder'.constantize
=> Notification::Builder
( Rails issue at github: https://github.com/rails/rails/issues/8726 )
ActiveRecord::Associations::Builder is a module in Rails. If you have a Notification::Builder, you can ask it its class:
>> Notification::Builder
=> ActiveRecord::Associations::Builder
>> Notification::Builder.class
=> Module
>> Notification::Builder.ancestors
=> [ActiveRecord::Associations::Builder]
Is this expected behavior?
Yes
OK, so... what choices do you have?
You can use a different term than Builder. Like Factory. or Notification::NotificationBuilder
More info:
* http://www.rubydoc.info/docs/rails/3.1.1/ActiveRecord/Associations/Builder/Association
* http://apidock.com/rails/ActiveRecord/Associations/Builder
This problem exists because you are using an ActiveRecord model as a namespace. I created a gist with some experimentation until I saw the root cause.
ActiveRecord models include the ActiveRecord::Associations module. Since you can get to a constant when including a module the Builder constant defined within Associations is now also reachable through the AR model. You will get this behavior with every class defined in the modules, which are included into an AR model:
1.9.3-p194 :010 > Post.ancestors
=> [Post(id: integer, title: string, published_at: datetime, created_at: datetime, updated_at: datetime), Post::GeneratedFeatureMethods, #<Module:0x007fec74dc33a0>, ActiveRecord::Base, ActiveRecord::Core, ActiveRecord::Store, ActiveRecord::Serialization, ActiveModel::Serializers::Xml, ActiveModel::Serializers::JSON, ActiveModel::Serialization, ActiveRecord::Reflection, ActiveRecord::Transactions, ActiveRecord::Aggregations, ActiveRecord::NestedAttributes, ActiveRecord::AutosaveAssociation, ActiveModel::SecurePassword, ActiveRecord::Associations, ActiveRecord::Timestamp, ActiveModel::Validations::Callbacks, ActiveRecord::Callbacks, ActiveRecord::AttributeMethods::Serialization, ActiveRecord::AttributeMethods::Dirty, ActiveModel::Dirty, ActiveRecord::AttributeMethods::TimeZoneConversion, ActiveRecord::AttributeMethods::PrimaryKey, ActiveRecord::AttributeMethods::Query, ActiveRecord::AttributeMethods::BeforeTypeCast, ActiveRecord::AttributeMethods::Write, ActiveRecord::AttributeMethods::Read, ActiveRecord::AttributeMethods, ActiveModel::AttributeMethods, ActiveRecord::Locking::Pessimistic, ActiveRecord::Locking::Optimistic, ActiveRecord::CounterCache, ActiveRecord::Validations, ActiveModel::Validations::HelperMethods, ActiveSupport::Callbacks, ActiveModel::Validations, ActiveRecord::Integration, ActiveModel::Conversion, ActiveRecord::AttributeAssignment, ActiveModel::ForbiddenAttributesProtection, ActiveModel::DeprecatedMassAssignmentSecurity, ActiveRecord::Sanitization, ActiveRecord::Scoping::Named, ActiveRecord::Scoping::Default, ActiveRecord::Scoping, ActiveRecord::Inheritance, ActiveRecord::ModelSchema, ActiveRecord::ReadonlyAttributes, ActiveRecord::Persistence, Object, PP::ObjectMixin, ActiveSupport::Dependencies::Loadable, V8::Conversion::Object, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
A possible solution is to use a module as a namespace. For example module Notifications.
I'm migrating a Rails app from 2.0.2 to 2.3.5.
I have a model class:
class EventQuery < ActiveRecord::Base
set_table_name 'EventQueries'
def to_s; name; end
end
I'm debugging the problem in the console. I try to assign an EventQuery object an attribute:
./script/console production
Loading production environment (Rails 2.3.5)
>> q=EventQuery.new
=> #<EventQuery id: nil, role: nil, name: nil, description: nil, query: nil>
>> q.role="ADM"
> TypeError: can't convert String into Integer
from /usr/lib64/ruby/gems/1.8/gem /activerecord-2.3.5/lib/active_record/dirty.rb:135:in `[]='
(...)
In Rails 2.0.2 it works fine:
./script/console production
Loading production environment (Rails 2.0.2)
>> q=EventQuery.new
=> #< EventQuery id: nil, role: nil, name: nil, description: nil, query: nil
>> q.role='ADM'
=> "ADM"
Any ideas? Thank you!
I'm now thinking this must be related to plugin record_modified which is part of the application.
This problem is related to a name conflict with a method in the record_modified plugin. The console works fine if I exclude the plugin from loading.
I have decided to remove the record_modified plugin and refactor the code. Instead of using RecordModified methods I'm using ActiveRecord as follows:
RecordModified.modified? --> ActiveRecord.changed?
RecordModified.changed_columns --> ActiveRecord.changes + minor refactoring