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.
What's a good conventional order for associations, scopes, attributes, validations, callbacks, non-standard code (acts_as_*), class and instance methods?
Also, if I need to split a model into modules, is it better to base these modules on features (Post::Comments, Post::Share, Post::Admin etc.) where each module also include the scopes and associations, or is it preferable to keep all scopes and associations in the base model and only split the methods?
You won't get a clear answer on this. It depends on your personal preference and what suits your code.
What I would suggest regarding the order in the model: pick one, and stick to it. It doesn't really matter if callbacks come before or after validations, as long as it makes some sense to you, and you can remember it.
And about the modules: I would pick two of the smaller models, and try both approaches and have a look what suits you better. Personally I don't like my scopes distributed amongst multiple files, but that does not have to be your preference too.
Maybe you want to have a look how others are doing it, e.g. https://github.com/gitlabhq/gitlabhq or https://github.com/diaspora/diaspora to help with your decisions.
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 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 know that using plural for controllers name is the right way in Ruby, but in some cases using singular for controllers name is more appropriately. For example
http://foobar.com/admin/login/
http://foobar.com/admin/dashboard/
http://foobar.com/profile/
I think you get what I mean.
So which are the best practices of using singular for controllers names ?
Any example will be appreciated !
If you don't need the whole scaffolding, for example you probably have no admin model, then you can just generate a controller with a singular name.
rails g controller admin
Then, you need to take care of the routing.
match 'admin/login' => 'admin#login'
Rails favours convention over configuration, so it means that controllers are ALWAYS using plural, table names also, whereas models are always using singular, with an uppercase at the beginning.
I would strongly advice you to stick to these conventions, that's best practice and in the foundation of the Rails framework
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 11 years ago.
I am starting with web development. I looked at the URL design of a few websites, especially github and stack overflow.
A user page on github is of the form github.com/<username> (e.g. github.com/roma1n). This strikes me as requiring careful reservation of top-level identifiers, i.e. there cannot be a user explore in github, because github.com/explore shows a list of trending/interesting repositories. It also seems hard to extend once usernames that would make useful page names (e.g. latest, all) have been taken up by users.
Stackoverflow seems to go on the opposite route, where a user page is of the form stackoverflow.com/<numeric id>/<username>. This seems to add redundancy to the address, unless multiple users with the same nickname are allowed (e.g. to make life easier when identifying through other providers such as OpenId?).
What are the pros and cons of each solution, (and of the other obvious ones such as example.com/users/<numeric_id> or example.com/users/<nickname>)? Is there a current best practice or reasonable default?
I think your suggestion example.com/users/<nickname> is pretty reasonable. given a choice I'd pick this, becuase it's more memorable for users than an id, and it sidesteps the the users nickname problem. of course you're free to adopt any convention that fits your use case, so whatever works!
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 have a Rails app with 600 models and that will soon grow to 800-1000. I'd like to segment the rails app so that only certain models get loaded and therefore act as a separate app, but all share the same base models. Is there a standard practice for doing this?
EDIT: I am on 2.3.8
EDIT 2:
The problem is that many models are similar, but different just enough that it warrants writing a new class, i.e. the logic required to put it all in one model would be horrendous. Some of the models could be moved out into rake tasks or the lib directory, but only about 30 or so. Some are abstract classes that act as parents of one arm of the model tree. However, most relate to database tables. I am thinking about at deploy time segmenting parts of the app into plugins via Engines so that one app can only handle one set of models (they are independent) but so that I can keep them all together in development and in one git repo for convenience. I"m going to go down this route unless someone else has a better idea, and I'll post back to let you know how it goes.
Dude, thats a pretty insane amount of models... anyways for handling complex logic and easily reuse them across other projects I would recommend to you the engines (from 2.3+ is part of Rails).
With that in place you can split your model in different modules (engines)
http://railscasts.com/episodes/149-rails-engines
Toño
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