I created a new Model in a Rails 3 application, along with a database table - but because I was not creating a controller or views I did not use a rails generator and just created the file by hand.
The contents followed exactly the format of other Model files and I was careful to use a Plural name for the database table and a Singular name for the model class.
But when I ran the application, either in the console or as a server, I kept getting an error reporting the Model name as an 'uninitialized constant'.
I double and triple checked everything but no luck getting it to work. Poking around the web I found mention of the Lazy Loading feature in Rails whereby classes are not loaded until they are needed.
Along with that was mention that using the command 'Rails.application.eager_load!' in the console might solve the problem. indeed it did - but only in the Rails console...
The problem turned out to be that the model FILE name was PLURAL. The Model class itself was singular - which is correct - but the lazy loading mechanism was using this (I assume) to define the filename in which to find the Model. Because my was plural it could not find the file and because it did not find a model it assumed that my Model name was a constant and then found that to be uninitialized.
It's a confusing error message and I wonder if there might be a way for Rails to figure out that I wanted a model. But I figure this is a side effect of the Rails machinery for inferring model, controller, etc. names.
Ah well... live and learn...
A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.
Showing posts with label Rails 3. Show all posts
Showing posts with label Rails 3. Show all posts
Thursday, September 27, 2012
Wednesday, November 9, 2011
Manipulating Model/Table/Controller Names in Rails
There are some very useful methods that operate on the names of your models, tables and controllers in Rails. I use them infrequently enough that I have to relearn them every time - so I'm putting them in here:
Given a string with the name of a model (e.g. Project), how can I create an ActiveRecord call on the fly using that model?
str = 'Project'
project = (str.constantize).find(1)
How can I get the name of the corresponding controller ?
controller = str.tableize
Given a string with the name of a controller, how can I get the name of the Class/Model ?
str = 'sales_contact'
model_name = str.camelize
I the string is plural:
str = 'sales_contacts'
model_name = str.classify
How do I do the reverse operation?
str = 'SalesContact'
name = str.underscore
Given a string with the name of a model (e.g. Project), how can I create an ActiveRecord call on the fly using that model?
str = 'Project'
project = (str.constantize).find(1)
How can I get the name of the corresponding controller ?
controller = str.tableize
Given a string with the name of a controller, how can I get the name of the Class/Model ?
str = 'sales_contact'
model_name = str.camelize
I the string is plural:
str = 'sales_contacts'
model_name = str.classify
How do I do the reverse operation?
str = 'SalesContact'
name = str.underscore
Friday, March 25, 2011
Rails 3 ActionMailer - defining default host
In Rails 3 ActionMailer is really easy to set up and use. Ryan Bates has a great Railscast on the topic that you should check out.
But I ran into one issue when creating emails that contain links back to my server.
Because you are sending an email to a remote machine, you need to include absolute URLs in your links.
You can define that in a config file but the example given in the Railscast is now deprecated, plus when I first tried it, my app was not picking it up and all my links were still relative.
You two things for this to work:
1: In your application.rb or environments/development.rb add a line like this in the configuration block, replacing craic.com with your domain
2: There are several ways to specify a url to pass into a link_to call in Rails - but only one seems to work.
Specify the urls for your links using <your_model>_url(<your_object>) - not <your_model>_path(<your_object>) and not url_for(...). I tend to use mymodel_path in links in regular web pages so I can't just cut and paste code into a mailer view.
Here is an example that works:
But I ran into one issue when creating emails that contain links back to my server.
Because you are sending an email to a remote machine, you need to include absolute URLs in your links.
You can define that in a config file but the example given in the Railscast is now deprecated, plus when I first tried it, my app was not picking it up and all my links were still relative.
You two things for this to work:
1: In your application.rb or environments/development.rb add a line like this in the configuration block, replacing craic.com with your domain
config.action_mailer.default_url_options = { :host => "craic.com" }
The application.rb file applies to all environments. Putting this in development.rb or production.rb lets you use different hosts for each environment.2: There are several ways to specify a url to pass into a link_to call in Rails - but only one seems to work.
Specify the urls for your links using <your_model>_url(<your_object>) - not <your_model>_path(<your_object>) and not url_for(...). I tend to use mymodel_path in links in regular web pages so I can't just cut and paste code into a mailer view.
Here is an example that works:
<%= link_to object.name, mymodel_url(object) %>
...produces...
<a href="http://craic.com/mymodels/1">My Object</a>
Subscribe to:
Posts (Atom)