In a rails / mysql app, there is a method that lets you check if an associated object has been loaded, or if your referencing it will trigger a database query:
a = Author.first
b.books.loaded?
But in mongoid, there is no such method. Is there another method in mongoid that does something similar?
Thanks for any help.
The method is
_loaded?
For some reason SO demands I add more filler, even though the above is the correct answer, so here you go. Here's the opening line from The Great Gatsby, which I love:
In my younger and more vulnerable years my father gave me some advice
that I’ve been turning over in my mind ever since. “Whenever you
feel like criticizing any one,” he told me, “just remember that
all the people in this world haven’t had the advantages that you’ve had.”
Related
After the big news yesterday, I've been trying to find a solid article about how to fix this issue with regard to different versions of Rails, and I'm unable to do so.
The best resource that I have found so far is https://gist.github.com/1978249#file_securing_rails_updates.md, but it only provides one solution: adding ActiveRecord::Base.send(:attr_accessible, nil) to the initializer. This is also the solution presented here at http://railspikes.com/2008/9/22/is-your-rails-application-safe-from-mass-assignment at a much earlier time.
However, at the same time, I remember seeing at another place that just turn on the configuration: config.active_record.whitelist_attributes = true should be suffice.
I am thoroughly confused, from all these different resources, I'm in need to decide between two solutions that doesn't have any reference to which versions of Rails they apply to.
Perhaps I had missed a generic article on the fix after the incident, but I had not found a single article on the rails blog that shows this. I was not able to find it elsewhere, could someone please enlighten me on this. Thanks!
I found this in the gist https://gist.github.com/1978249
Add the following initializer:
config/initializers/disable_mass_assignment.rb
ActiveRecord::Base.send(:attr_accessible, nil)
Looks like a temporary fix to me until rails core comes up with something better !!
Forgive me if I've got my terminology wrong; I am still quite new to Ruby and Rails.
For school I am working on an RoR project as part of a team. I was pair programming with a teammate, who actually happens to be experienced in RoR; and I wrote something like the following:
d = self.deliverables.find_all_by_lifecycle_id(self.lifecycle_id)
My pair programming partner stopped me at this point and explained that this wouldn't work, because find_all_by_lifecycle_id could only be resolved successfully if called by the Deliverable model, which inherits from ActiveRecord::Base (which is, in turn, the class responsible for providing such magical functionality). The self.deliverables method returns an array, which doesn't offer the same capabilities. The alternate solution he suggested was to use a named scope.
I protested: "But... I'm pretty sure I tried it already... and it worked."
Sure enough, it does seem to work, as we discovered when we tested it out just to humor me. What was my teammate missing? I certainly don't have the expertise to see what could've been wrong about what he said (it made sense to me).
I take it, self.deliverables is defined by :has_many association. In this case, it's not just a simple array. Some of the things you can do with such collection are highlighted in the official rails guide. In particular, check part 4.3.1.10 collection.find(…):
The collection.find method finds objects within the collection. It uses the same syntax and options as ActiveRecord::Base.find.
It doesn't mention find_by_xxx syntax explicitly, but like you I've found it works in practice.
I'm considering using mongodb on a new project, but before delving in, I'd like to know if it supports some key features. I know you don't need migrations, and you can add embedded objects,...but does all that mean it also behaves as if the 'accepts_nested_attributes_for' method is always there?
Do you know of any other killer features that I should be aware of that would sway me for or against MongoDB?
Here's a recent article I'm aware of, and that others may also be interested in:
http://railstips.org/blog/archives/2009/12/18/why-i-think-mongo-is-to-databases-what-rails-was-to-frameworks/
Looks like the mongomapper guy is working on a solution to this:
http://groups.google.com/group/mongomapper/browse_thread/thread/e92c35f6e2302687/b9f0860ac83d9b4e?lnk=gst&q=nested#b9f0860ac83d9b4e
http://gist.github.com/275594
Does anyone know if there is a Rails gem/plugin/tutorial that shows how to create a Badge/Achievement system similar to what stackoverflow uses.
Thanks.
You might also want to try the achievements gem: https://github.com/mrb/achievements
It's based on Redis, so you'll need to get that working first. Basically, you define a bunch of achievement contexts (pages viewed, messages sent, etc.) along with multiple levels if necessary. Then, you increment your value appropriately upon certain events, and you can then check if the achievement has been reached.
This link also has a relatively detailed explanation of the thinking behind a badge/achievement system: RoR Achievement System - Polymorphic Association & Design Issues
check out https://github.com/paulca/paths_of_glory
I think it's less a framework but a design question. If you know how to build it in an object-oriented way, you'll eventually know how to build it in Rails too.
If you're a Rails newbie, check out the Rails Guide on "Active Record Associations" and try to identify the models and the associations of your "badge/achievment system".
Besides that: No, I don't know of any turnkey-gem/plugin/tutorial that would help you build such a system.
There is also Gioco, which I haven't yet tried:
http://joaomdmoura.github.io/gioco/
I'm looking for a rails plugin/gem which brings the functionality of marking an ActiveRecord-Model deleted, instead of deleteing it.
Does anybody know, what gems or plugins are up to date? (AAP is out-dated and is_paranoid doesn't appear to be used by the community).
Do you know alternatives?
It seems even the authors of both acts_as_paranoid and is_paranoid aren't using their respective plugins/gems any more. Both are using named scopes.
Yeah, it's not automagic or anything, but sometimes being explicit about your intentions is a good thing.
For completeness, here is a more recent gem for this purpose:
Paranoia - acts_as_paranoid for Rails 3
https://github.com/radar/paranoia
And another:
https://github.com/JackDanger/permanent_records
is_paranoid doesn't appear to be used by the community..
http://chadfowler.com/blog/2009/07/08/how-ruby-mixins-work-with-inheritance/ - Just a blog post the other day talking about it. Seems like it solved Chad's problem just fine (as well as lead him to write a post about inheritance and mixins).
How about you just have a valid:boolean column/attribute and set it to false when you wish to soft-delete the model? Or am I missing something?