I posted this last week, but far down in the comments. I get the top here, so here goes. We could maybe move this to a forum somewhere.
Here goes:
I want to quote Ryan From the comments in another cast
" - However I hope to, at some point, have more time to dedicate to Railscasts and make it my full time job."
I think railscasts is very valuable. I use it all the time as a reference guide.
I think it would be a great thing for the rails community if Ryan can have railscasts a his full time job.
Does anybody agree?
Can we all pull together and come up with suggestions on how railscasts can be free and Ryan can get a full time job income?
Maybe big rails companies can, under a voluntary contract pay Ryan some money every month?
I guess they all benefit a great deal from these casts?
Deciding whether an association is relational or embedded is tough. Another criterion to add to Ryan's suggestion is whether the number of that associated model will grow to very large.
In case of comment, though you can have thousands sometimes, after awhile, the article becomes stale and very few new comments will be added and so an embedded association works well.
But take another example: users and tasks. Because some user will not likely to stop having new tasks, and an embedded association will always be loaded when calling the user (I could be wrong about this, if so this is not a concern then), so the loading time of a user model with great number of tasks will be quite slow.
In such cases as these, I wonder if there is a 'limit' operator to be put in the query to reduce the # of embedded models returned.
MongoDB (and similar) looks like the bomb! Is this the death of traditional DB:s? Or is performance a bottle neck?
Anyway, about embeded assosciations I can see a problem when for instance on a first page you'd want to present the latest comments on all articles. This wouldn't be possible would it if the comments were to embedded?
Also! About the key, what if I want to let my users change their names? Is it possible to also update the key in such case?
Thank you!
ps. I would defintely pay for Railscast if it was subscription based. Maybe like $5 per month. ds.
@Erik
About showing the latest comments (that is to say show all articles' last comment), I used to have similar fears as well. But then I stepped back and thought, how would I do it if it were on activerecord? In Activerecord, then, one would probably do well with aggregating all articles first, calling the first of comments ordered in desc time, which is same as in Mongoid.
If we were to display the absolute latest ONE comment, I can see the concern. And to solve it, allow me to attempt here, I would find the latest changed article because comment is embedded, adding a comment to an article is classify as the same act as updating that article, so that latest changed article should then contain the latest comment.
I like what Ryan is doing and I agree with the posters above. I've actually been planning on donating and this just prompted me to action: I donated via PayPal.
Also, I don't see how big Rails companies will benefit from donating to Ryan, its more the indy devs and upcoming devs who'll get the most out of this.
Thanks
P.S. not that interested in Mongo, but I did enjoy and found useful many of the previous topics
@Erik: MongoDB performance is great, it's way faster than Mysql. I played around a bit and inserted 100.000 records in both Mongodb and Mysql on my MacBook and Mongodb finished the job in 1/3 of the time it took Mysql. However, MongoDB has some caveats you should know about: no transactions & no single server durability.
Keys are not meant to updated, so if you want your users to change their name, you will have to generate a slug (there is a gem for that).
@dotemacs: the bigger rails firms benefit from a vibrant rails community. I think that many newcomers and semipro rails developers learn much from the railscasts. This improves the pool to requite employees from. They also use open source plugins and gems for rails. In many cases the ones that makes these also get tricks form railscasts. I can send emails to some companies and ask them if there is any interest to donate Ryan some money every month.
If 10 firms donate 100$ every month, it could be a start.
I really would like to read more ideas. Be creative! We all can donate, but I do not think that will be a steady income source.
I agree with previous comments that choosing between embedded and relational association is hard. I usually end up with the former.
The thing you *have to* keep in mind is also the BSON limit on document size. It's currently only 4MB, so you can imagine someone breaking your document with adding more data than allowed -- think about validates_length_of or something like that.
If you want a complete example application using Mongoid, take a look on GitHub at http://github.com/fortuity/rails3-mongoid-devise.
The example app integrates Mongoid with Devise, to give you quick development without schemas or migrations plus ready-made authentication. Gives you a skeleton or starter app you can deploy in minutes. Best of all, it's got a detailed tutorial (walk-through) to show how it's built. Follow the link above to see more.
Great to see a Railscast on Mongoid! Thanks, Ryan!
I set this up and then wrote a little loop that .creates an object. It seems to go at about 10 saves per second -- are there config options or something I need to get the legendary MongoDB write performance?
To answer my own question from comment 17, it was slow because I was creating objects on an association. Here's the comparison and a faster way to do the same:
http://gist.github.com/659961
Excellent as always. But I do think you are copying Steve Job's style. I heard you say awesome like 4 or 5 times in 10 minutes. Yes the iPad is a magical device and Mongoid is truly awesome. :)
Thanks a lot for the episode. I'm starting to use mongoid and looking for a solution of a query case: formate a Datetime field to be like "YYYY/MM/DD" and group by this field, and then select the max value of another Integer field of each group. This should not be difficult for a sql datebase. Went through mongoid documentation, but found nothing helpful.
I would like to return all the comments with the name equal to "test"
When i call Comment.where(:name => 'test').length, it returns 0
When i call Comment.all, it returns 0.
Article.where("comments.name" => 'test') works but it return all the articles contain comment with title equals 'test'
We started using Mongoid and then eventually switched to Mongomatic. It's leaner, quicker, and reading the code is so straightforward! Mongoid breaks in ways you can't even imagine, problem is you always get bit in the ass when your collection is big enough for it to be a pain to replace. As soon as we hit a few million documents, performance started to nose-dive. Now running Mongomatic with our own extension, went past the 100 mil documents mark a few weeks back, would never look back.
If you want to use Mongodb to its full potential, don't let Mongoid spoil the experience.
You describe how to create new Article and then add Comments.
What if i need to create new Article with Comment at the same time? For this app it's weird but in my case i don't know how to show form for new Article with fields for Comment.
Could you help me with this?
It makes sense to the comment document have an author which is a user, right? How would one go about that? Is is possible to have this "cyclic" reference? (User has many posts has many comments has one User)? How to do that?
is it me only or someone else facing problem with multi parameter attributes problem which will return nil when Article.find("4da14b1447640b14eb000002").published_on but with published_on: nil, published_on(1i): "2011", published_on(2i): "6", published_on(3i): "10"
Hi Ryan, I have watched all your railscasts and I think you are doing a great job of helping people like me who are new to Rails.
I followed your tutorial and it works great. But, when I try to to the same thing for namespace routes I keep getting errors.
How do I create the same app within a namespace?
I have routes as:
namespace :admin do
resources :blogs do
resources :comments
end
end
and in the Show.html.erb I have:
New Comments
<%= form_for [:admin, @blog, Comment.new] do |f| %>
<%= f.label :body %> <%= f.text_field :body %>
<%= f.submit %>
<% end %>
Thank you djensen47 for pointing out the need to add
include Mongoid::MultiParameterAttributes
in model definitions when using Date or DateTime. Without it, the datetimes were being persisted as a set of discrete values (one each for day, month and year parts) and mongoid could not corece them back into a DateTime for display etc.
BTW I'm using mongoid 2.4.0 with Rails 3.1.3 and Ruby 1.9.3p0 and it did not work correctly until I made your addition. Cheers!
on the show page for route article/article_id, you coded a call to the comments controller index action to iterate through all the associated comments.
how do you code the show, edit, update actions in the article controller for individual line item comment manipulation?
What I'm really having a problem with is embeds_one associations. Can't find any tutorials or docs on it. I can find enough on embeds_many but not what I need.
Hello all, Is there any demo application of implementing Riak database using Risky library in Rails application? I've googled alot but didn't find any. Even there is no episode in railscast for the same.
Hello all, Is there any demo application of implementing Riak database using Risky library in Rails application? I've googled alot but didn't find any. Even there is no episode in railscast for the same.
I posted this last week, but far down in the comments. I get the top here, so here goes. We could maybe move this to a forum somewhere.
Here goes:
I want to quote Ryan From the comments in another cast
" - However I hope to, at some point, have more time to dedicate to Railscasts and make it my full time job."
I think railscasts is very valuable. I use it all the time as a reference guide.
I think it would be a great thing for the rails community if Ryan can have railscasts a his full time job.
Does anybody agree?
Can we all pull together and come up with suggestions on how railscasts can be free and Ryan can get a full time job income?
Maybe big rails companies can, under a voluntary contract pay Ryan some money every month?
I guess they all benefit a great deal from these casts?
Seems like he's doing fine now. Railscasts has become a great service. Great job Ryan.
Many thanks Ryan, just what I need !
Fully agree with Andreas Lyngstad post !!!
Deciding whether an association is relational or embedded is tough. Another criterion to add to Ryan's suggestion is whether the number of that associated model will grow to very large.
In case of comment, though you can have thousands sometimes, after awhile, the article becomes stale and very few new comments will be added and so an embedded association works well.
But take another example: users and tasks. Because some user will not likely to stop having new tasks, and an embedded association will always be loaded when calling the user (I could be wrong about this, if so this is not a concern then), so the loading time of a user model with great number of tasks will be quite slow.
In such cases as these, I wonder if there is a 'limit' operator to be put in the query to reduce the # of embedded models returned.
Thank You, Ryan, for the screencast
MongoDB (and similar) looks like the bomb! Is this the death of traditional DB:s? Or is performance a bottle neck?
Anyway, about embeded assosciations I can see a problem when for instance on a first page you'd want to present the latest comments on all articles. This wouldn't be possible would it if the comments were to embedded?
Also! About the key, what if I want to let my users change their names? Is it possible to also update the key in such case?
Thank you!
ps. I would defintely pay for Railscast if it was subscription based. Maybe like $5 per month. ds.
@Erik
About showing the latest comments (that is to say show all articles' last comment), I used to have similar fears as well. But then I stepped back and thought, how would I do it if it were on activerecord? In Activerecord, then, one would probably do well with aggregating all articles first, calling the first of comments ordered in desc time, which is same as in Mongoid.
If we were to display the absolute latest ONE comment, I can see the concern. And to solve it, allow me to attempt here, I would find the latest changed article because comment is embedded, adding a comment to an article is classify as the same act as updating that article, so that latest changed article should then contain the latest comment.
Hello,
I like what Ryan is doing and I agree with the posters above. I've actually been planning on donating and this just prompted me to action: I donated via PayPal.
Also, I don't see how big Rails companies will benefit from donating to Ryan, its more the indy devs and upcoming devs who'll get the most out of this.
Thanks
P.S. not that interested in Mongo, but I did enjoy and found useful many of the previous topics
I agree with Andreas Lyngstad. Awesome job Ryan. I'd love more casts :) You have a gift for this.
As usual, great Episode Ryan!
@Erik: MongoDB performance is great, it's way faster than Mysql. I played around a bit and inserted 100.000 records in both Mongodb and Mysql on my MacBook and Mongodb finished the job in 1/3 of the time it took Mysql. However, MongoDB has some caveats you should know about: no transactions & no single server durability.
Keys are not meant to updated, so if you want your users to change their name, you will have to generate a slug (there is a gem for that).
@dotemacs: the bigger rails firms benefit from a vibrant rails community. I think that many newcomers and semipro rails developers learn much from the railscasts. This improves the pool to requite employees from. They also use open source plugins and gems for rails. In many cases the ones that makes these also get tricks form railscasts. I can send emails to some companies and ask them if there is any interest to donate Ryan some money every month.
If 10 firms donate 100$ every month, it could be a start.
I really would like to read more ideas. Be creative! We all can donate, but I do not think that will be a steady income source.
Thanks for the episode!
I agree with previous comments that choosing between embedded and relational association is hard. I usually end up with the former.
The thing you *have to* keep in mind is also the BSON limit on document size. It's currently only 4MB, so you can imagine someone breaking your document with adding more data than allowed -- think about validates_length_of or something like that.
Hey Ryan! Thanks again. Would you recommend using MongoDB for newbies to rails?
Thanks, Ryan! Mongoid is very very awesome. One of the best-written ruby libraries out there, if you ask me.
Keep up the great work on Railscasts!
If you want a complete example application using Mongoid, take a look on GitHub at http://github.com/fortuity/rails3-mongoid-devise.
The example app integrates Mongoid with Devise, to give you quick development without schemas or migrations plus ready-made authentication. Gives you a skeleton or starter app you can deploy in minutes. Best of all, it's got a detailed tutorial (walk-through) to show how it's built. Follow the link above to see more.
Great to see a Railscast on Mongoid! Thanks, Ryan!
Ryan, you are awesome, truly one of the perfect examples of use of emerging technologies with the spirit of innovation & open source.
Can't tank you enough to be one of the leaders on my learning curve & inspiration in the Ruby & Rails world. Especially with expertise & agility.
Keep doing the great work.
That was great tutorial. After this I would love to see integration of mongoid with cucumber and pickle and it's steps.
I set this up and then wrote a little loop that .creates an object. It seems to go at about 10 saves per second -- are there config options or something I need to get the legendary MongoDB write performance?
Great one.. as always.
Thanks
To answer my own question from comment 17, it was slow because I was creating objects on an association. Here's the comparison and a faster way to do the same:
http://gist.github.com/659961
Excellent as always. But I do think you are copying Steve Job's style. I heard you say awesome like 4 or 5 times in 10 minutes. Yes the iPad is a magical device and Mongoid is truly awesome. :)
Thanks for all the episodes, they are all very useful.
Thanks a lot for the episode. I'm starting to use mongoid and looking for a solution of a query case: formate a Datetime field to be like "YYYY/MM/DD" and group by this field, and then select the max value of another Integer field of each group. This should not be difficult for a sql datebase. Went through mongoid documentation, but found nothing helpful.
In def create, what if you have a respond_to block? For example:
respond_to do |format|
if @comment.save
....
How do you check if the save was successful?
Currently you are using create: @comment = @article.comments.create!(params[:comment]), which doesnt return true or false
in follow-up to my post at #30. In case you are wondering, I need the respond_to block for ajax:
format.js { flash[:notice] =
'Comment was successfully
created.' }
which also calls create.js.erb for further processing
Ok, figured it out. build() instead of create() seems to invoke save() for the respond_to block
Good one, Thank you.
I've been using MongoID for a couple of months now, and I love it!
One thing I like particularly about MongoID is that it uses
include , rather than sub-classing, to add the persistence to a class!
That's much cleaner, and allows you to use sub-classing when you really need it for _your_ models/application..
I've watched pretty much every single show you've done and this is probably the best one yet.
Thanks
Hello,
I would like to return all the comments with the name equal to "test"
When i call Comment.where(:name => 'test').length, it returns 0
When i call Comment.all, it returns 0.
Article.where("comments.name" => 'test') works but it return all the articles contain comment with title equals 'test'
anyone knows how to do it ?
We started using Mongoid and then eventually switched to Mongomatic. It's leaner, quicker, and reading the code is so straightforward! Mongoid breaks in ways you can't even imagine, problem is you always get bit in the ass when your collection is big enough for it to be a pain to replace. As soon as we hit a few million documents, performance started to nose-dive. Now running Mongomatic with our own extension, went past the 100 mil documents mark a few weeks back, would never look back.
If you want to use Mongodb to its full potential, don't let Mongoid spoil the experience.
You describe how to create new Article and then add Comments.
What if i need to create new Article with Comment at the same time? For this app it's weird but in my case i don't know how to show form for new Article with fields for Comment.
Could you help me with this?
Has anyone else had issue using the key value?
I've been trying to get that to work, but can't figure it out.
gem "mongoid", "2.0.0.rc.7"
gem "bson_ext", "~> 1.2"
user 718: I've got the same problem.
It's fixed according to the mongoid author, but I'm not sure that fix is already pushed in rc 7
hey there, let me ask you something...
It makes sense to the comment document have an author which is a user, right? How would one go about that? Is is possible to have this "cyclic" reference? (User has many posts has many comments has one User)? How to do that?
I have a similar question to user 884.
this is what i'm trying to do.
https://gist.github.com/833459
a comment has one author but can have multiple recipients.
Great tutorial thanks a lot!
I just had one "bug": I had to add manually
resources :articles do
resources :comments
end
in my routes files. I'm new to rails and I struggle a little to understand the way routes works but this solved it for me.
Hey /users/1005 you could read up a cool book "The Rails way" it has got some nice insight on use of nested models and resources... :)
is it me only or someone else facing problem with multi parameter attributes problem which will return nil when Article.find("4da14b1447640b14eb000002").published_on but with published_on: nil, published_on(1i): "2011", published_on(2i): "6", published_on(3i): "10"
In Mongoid 2.0 (and I believe 2.0.1 as well) you need to include the following in your model if you're going to use Date or DateTime
include Mongoid::MultiParameterAttributes
Source: https://github.com/mongoid/mongoid/issues/30#issuecomment-1211911
Ryan, maybe it'll be useful to include the routes.rb file in the show notes.
Hi Ryan, I have watched all your railscasts and I think you are doing a great job of helping people like me who are new to Rails.
I followed your tutorial and it works great. But, when I try to to the same thing for namespace routes I keep getting errors.
How do I create the same app within a namespace?
I have routes as:
namespace :admin do
resources :blogs do
resources :comments
end
end
and in the Show.html.erb I have:
New Comments
<%= form_for [:admin, @blog, Comment.new] do |f| %>
<%= f.label :body %> <%= f.text_field :body %>
<%= f.submit %>
<% end %>
Hello - Excellent tutorial.
Thank you djensen47 for pointing out the need to add
include Mongoid::MultiParameterAttributes
in model definitions when using Date or DateTime. Without it, the datetimes were being persisted as a set of discrete values (one each for day, month and year parts) and mongoid could not corece them back into a DateTime for display etc.
BTW I'm using mongoid 2.4.0 with Rails 3.1.3 and Ruby 1.9.3p0 and it did not work correctly until I made your addition. Cheers!
great. but when I typed "mongo" in the search box on the main page I can not find this episode (
Is there a way to completely get rid of active record and sqlite gem?
edit:
''' bash
rails new appli --skip-active-record
'''
@djensen47 Your advice is still useful in Rails 3.2.3 and Ruby 1.9.3-p125 to make the
published_on
field show up the date. Thx!Sorry if this was posted before
but I would recommend adding --skip-active-record after your rails new MyApp command. You will save a lot of troubles.
Best regards.
on the show page for route article/article_id, you coded a call to the comments controller index action to iterate through all the associated comments.
how do you code the show, edit, update actions in the article controller for individual line item comment manipulation?
Nevermind, figured it out this morning.
In wich differs the revised episode from this one? I might do my first subscription purchase if you give a good reason :)
btw, thanks for all these screencasts, you are awesome Mr. Ryan Bates!
How would you add a
:destroy
function to the embedded comments?Also, any examples on doing this with Carrierwave?
What I'm really having a problem with is
embeds_one
associations. Can't find any tutorials or docs on it. I can find enough onembeds_many
but not what I need.Anyone have a lead?
Hi Ryan,
Can you please update the episode as it is for Ruby 1.9.2 and rails3.0.1.
It is throwing error for "ActionController::RoutingError (undefined method `referenced_in' for Article:Class" when using rails 3.2.9 and ruby 1.9.3
Thanks,
Vardhan.
You're supposed to override the _id field:
Docs: http://mongoid.org/en/mongoid/docs/upgrading.html
Hello all, Is there any demo application of implementing Riak database using Risky library in Rails application? I've googled alot but didn't find any. Even there is no episode in railscast for the same.
Hello all, Is there any demo application of implementing Riak database using Risky library in Rails application? I've googled alot but didn't find any. Even there is no episode in railscast for the same.
With rails4 you must specify the parameters that are being saved.