As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In Rails, we offen write a lot of code in our models such as class methods, plugin methods, named_scopes, callbacks... I am wondering if there is a good pattern to organize the sequence. I saw the best example in a presentation before, but now I forgot.
Anyone have suggestion? Thanks
There is no set way... If you are using a scope (since Rails 3, named_scope is deprecated) that relies on a method, it has to be defined after the method in the model. It's possible to mix and match and sometimes it's necessary to do so.
It doesn't affect load time or efficiency to the best of my knowledge
I'm pretty OCD when writing ruby, so I have a very opinionated answer to your question. I created this gist as an example of the structure we use.
I wrote about that a while back as part of my style-guide:
acts_as_good_style.
YMMV as to what the "best" grouping/ordering is, but if you'd like my take it's there under the "Model idiom" section
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm looking for a very concise guide to using rails. I've done lots of work with other similar frameworks like Django so I just need a very short tutorial. Does anything like that exist?
Thanks!
This is best guide for those who are new to rails..
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
If you're willing to put up with a few quick video tutorials, Rails for Zombies by EnvyLabs (railsforzombies.org) gives a good overview of the framework.
If you decide to check it out, know this: at the time of this writing, there's one error in the tutorials. The code they show you for writing a basic redirect is missing a prepended slash on the URI. (At least, according to the interpreter they use for the accompanying exercises, it's incorrect.)
Other than that small gripe, in my opinion it's a great way to onboard yourself and colleagues in Rails, before drilling down into the gritty details.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I started learning ruby on rails few weeks ago. I don't completely understand metaprogramming yet, but first I want to ask whether metaprogramming is worth learning if I only want to use ruby on rails to build websites. The example I see for metaprogramming is for generating undefined class method on the fly, but is it necessary?
My background: I use python on a daily base for scientific computing and have limited experience with django.
Metaprogramming is by no means a requirement to writing websites.
If you're beginning to program in Ruby, it's probably best not to worry about it until you're much more familiar with the language. The added flexibility it affords you comes at the expense of complexity and obscurity.
It depends entirely on the functionality of the website. Learn the basic idea of meta programming , then carry on with what you're doing. You'll then know if you are trying to solve something that meta programming would help with, and you can dig in more.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
When writing ruby (& ror) applications with BDD, should the step definitions be written before you even create the models, views and controllers in order to save the maximum headache?
Yes, you should - that's the main idea of BDD and TDD, respectively. Red - Green - Refactor. First start off with your feature (e.g. use Cucumber here), then write the step definitions and finally your code.
The RSepc Book is a good read to capture the whole thing. Take at look at figure 1.1 The BDD-cycle on page 29. This gives you a good overview on how to approach BDD and TDD and also has some nice examples.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I m new to ROR3.0 I was read a lot place saying that is "this code is DRY" or something related to DRY
I want to know how to make a code DRY ?
Is it necessary my code should be DRY?
DRY stands for Don't Repeat Yourself. If it's possible, it's good practice to use DRY in most coding environments to make it easy to maintain going forward (and stop your copy-paste keys getting worn out!)
I'd suggest wherever code is repeated to extract the common code and extract into a method.
The wiki below is a good place to see where a lot of these shorthand expressions came from:
http://c2.com/cgi/wiki?DontRepeatYourself
In this case, 'DRY' simply means 'Don't Repeat Yourself'. This simple guideline leads to writing smaller, better decomposed methods which can be reused in several contents. This in turn leads to easier maintenance, better testability etc.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I like to use the strongly defined routes like
#Html.RouteLink("Home", RouteName.HomeIndex)
Why there is no equivalent helper for child actions? I don't like the actual #Html.Action(actionName, controllerName, routeValues) helper.
In addition, the performance would be better. There is no need to check all routes internal in the RouteCollection.
Contribute your idea and even the helper at http://aspnet.codeplex.com/ - it's open source.
The answer to Why isn't there one is that either:
Nobody thought of it yet
Someone thought of it but didn't do anything about it
Someone thought of it but thought better of it - for example because a route can be very general and you would need to supply controller, action and routevalues for most routes, which means the Action helper works fine.
In terms of performance, I've not had any problems with the performance of Html.Action so you might want to measure it before you get too concerned about optimising this aspect of the framework.