I've been trying to find the source code so I can understand how ActiveRecord dynamic attribute-based finders work. I haven't been able to find where in the source code the definitions live.
Could someone put me to the location in the file(s) that define this functionality? Thanks!
Using Rails 3.2.5
I looked in ActiveRecord::Model and noticed that there was a module included called DynamicMatchers. It looks like this is where all the dynamic attribute finders come from. Essentially it's using method missing and reflecting back on the associated model:
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/dynamic_matchers.rb
Related
I was reading the ActiveSupport core_ext source, and saw that it directly open and extend the core ruby class, e.g: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/conversions.rb
. Doesn't that make it harder for us to know wether some method is from activesupport or actually provided by ruby itself (e.g via Method#owner)?
Why doesn't it use something like prepending/including a module to give its added functionality? E.g:
module StringConversionExtension
def to_time
# some implementation
end
end
String.prepend(StringExtension)
Is there any historical/performance reason the implementation is as it is now?
Why doesn't it use something like prepending/including a module to give its added functionality? E.g:
Possibly to either:
Minimize the amount of code in Rails (since your proposed approach is a bit more verbose), or
Minimize the number of methods/constants/modules in existence as well as in the core classes, since in your proposed approach the core String class would have the new method and it would now be including the StringExtensions module.
I am using namespacing in my Rails 5 app to try to keep resources organised.
I've been generating resources using the command line by adding the namespace folder to the generate command.
This makes a folder in the models folder for the main folder which the namespaced files are saved in.
I've since been reading posts from others that suggest namespacing models is not a good idea.
An example of what I currently have is:
class Stance::Assessment < ApplicationRecord
It seems to work alright so far.
What is the problem with namespacing models?
If it is a problem, does that mean I can't organise my models into folder groups, or does it mean that the model class doesnt need to be named wiht the "Stance::"?
There is a certain cost of complexity involved with "namespacing" your models. Ruby does not actually have true namespaces. Rather it has has modules which provide encapsulation.
Rails and ActiveRecord was designed around placing your application code in the Main object (the global object). While this might seem like a bad practice it is very simple and works well with the convention over configuration approach. It also allows a much simpler autoloading scheme and avoids the need to nest every single file in an additional folder.
Namespacing does have great organizational merits though and lets you avoid collisions. But there are a few minor aches in the backside:
table prefixes, having the generated table names like my_app_projects_tasks is really inconvenient when you need to write a custom join.
You need to override ActiveModel::Naming so that it does not look for paths like my_app_projects_tasks_path when using the polymorphic route helpers.
You need to explicitly set the class_name option when creating associations or override how ActiveRecord resolves constant names.
you can prefix your models, so instead of Stance::Assessment you would have StanceAssessment. not as clean as a namespace but it's pretty close to it
I'm a total noob to rails and I'm trying to understand a project I'm working on. I've found a method named cv_member_url in one of the views, but I can't for the life of me figure out where it is defined... One thing I do know about rails is that it is a very flexible language so it could be some sort of gem creating this method.
Any ideas where this method may have come from? (or better yet, how I can add others)
Thank you!
Do you have a model called CvMember? If so, the method is probably a named route for that model. See here for more info:
http://guides.rubyonrails.org/routing.html#paths-and-urls
To see all your named routes, you can run
rake routes
Those are named routes which are automatically defined based on what you have in routes.rb. *_url should be used in the controller, and *_path should be used in the views. Here's some more info from the official rails guide.
Assuming you can run this in development: You should put a breakpoint on it and step inside. Most likely it is dynamically defined or maybe in plugin.
I'm new to rails and can't figure out this issue...
I have a controller
Admin::Blog::EntriesController
defined in app/controllers/admin/blog/entries_controller.rb
And I have a model called
Blog::Entry
defined in app/model/blog/entry.rb
When I try to access my model from the controller, I get a "uninitialized constant Admin::Blog::EntriesController::Blog" from this line:
#blog_entries = Blog::Entry.find(:all)
Clearly it is not finding the namespace correctly which is odd because according to what I have read, I have placed my model in the correct folder with the correct syntax.
Any ideas on how I can fix this?
Thanks
Try:
#blog_entries = ::Blog::Entry.find(:all)
It's currently looking for the wrong class. Using :: before Blog will force it to look from the top level.
It is now 2011 and we are in Rails 3.1 territory, but this issue still arises. I just ran into it with a namespaced controller referencing a non-namespaced model, but only when there were no rows for that model in the database!
Prefixing the model name with :: fixes the problem.
You can achieve a custom table name by using
set_table_name('foo')
at the top of your model.
As for multiple namespaces, you might be able to get away with using
polymorphic_path(#the_object)
to generate your urls as it does more basic inference (in my experience at least, maybe form_for uses it under the hood).
Yeah, from looking at the code form_for uses polymorphic_path under the hood.
I came across a controller in an older set of code (Rails 1.2.3) that had the following in a controller:
class GenericController > ApplicationController
# filters and such
model :some_model
Although the name of the model does not match the name of the model, is there any reason to specify this? Or is this something that has disappeared from later versions of Rails?
This had to do with dependency injection. I don't recall the details.
By now it's just a glorified require, which you don't need because rails auto-requires files for missing constants.
Yes, that is something that has disappeared in later versions of Rails. There is no need to specify it.