Specifying model in controller? - ruby-on-rails

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.

Related

Default namespace of models in Rails

After upgrading my Rails app, I have run into a problem where the name of one of my models are conflicting with another class in Rails, namely Configuration.
Is there a way for me to (from a controller) explicit use my model class Configuration instead of ActiveSupport::Configurable::Configuration? What is the default namespace for my models?
You can use ::Configuration to call your class. It means that you're referring to the constant Configuration from the toplevel namespace. But I think it's annoying. You can rename your class to avoid this.

How to move back from inherited resources gem

Recently I scaffold-ed two of my models/controllers/view, let's call them xxx and yyy. Now I look under the controller file, I see absolutely nothing ! but it was still functioning, upon investigation I found that was due to the
inherited_resources Gem
So the controllers look like this currently
class xxx < InheritedResources::Base
end
so if I change
InheritedResources::Base to ApplicationController
like I have it other controller, would it behave like normal controller ? I tried looking up on the docs, I was referred here for questions.
What is the best way to get normal controller/models back for those two scaffolds ?
Thanks for your time and help.
in config/application.rb add:
#use rails scaffolding generator
config.app_generators.scaffold_controller = :scaffold_controller
When you use InheritedResources, the gem registers a controller generator that generates just that, your controller definition. The point of using InheritedResources::Base is to clean your controllers and move all of the seven REST actions to a common class. You don't need to define any of the following methods if you extend InheritedResources::Base
index
new
create
edit
update
show
destroy
They're all the defined for you. Go ahead and make a test, scaffold a resource and go to it's index method, add a couple records, play around...
Now if you really want to go back to the old way and have your code generated by the bundled controller generator, remove inherited_resources from your Gemfile, bundle install, and generate your scaffolds again.
Hope it helps (:
P.S. if you decide to use InheritedResources (which I suggest you do) you can overwrite any methods you want to customize. Have a look at the docs, everything is more clear over there.

How do ActiveRecord dynamic attribute-based finders work?

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

Uninitialized content when trying to include ActionController::UrlWriter in model

I'm using Rails 3 beta 4 and trying to include ActionController::UrlWriter in my model, which is the correct way to go about it as far as i can tell, but i get "Uninitialized Constant ActionController::UrlWriter".
Any idea why that would be? Did it move in rails 3?
First I agree with zed. This shouldn't be done in a model. Your models should be unaware of any http url.
I do the same in a resque job. Here's what I do :
include ActionDispatch::Routing::UrlFor
include ActionController::PolymorphicRoutes
include Rails.application.routes.url_helpers
default_url_options[:host] = 'example.com'
Then you can call any usual url generator.
url_for(object)
page_url(object)
It'll create the link on the host defined as default_url_options[:host]
Answers to Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models? are pretty good
or see
http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html
http://siddharth-ravichandran.com/2010/11/26/including-url-helpers-in-models/
Basically, you can do something like this in a model:
def url
Rails.application.routes.url_helpers.download_attachment_path(self)
end
It is worth considering whether this is the right layer, though. In my case it's for file attachments and I want to do attachment.url instead of writing out a helper a lot.

Ruby on Rails model inside namespace can't be found in controller

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.

Resources