I am using Ruby on Rails to create a website for a game I play.
I have a User model and a Starbase model. The relationship I am trying to setup is like so
class User < ActiveRecord::Base
has_many :starbases
end
class Starbase < ActiveRecord::Base
belongs_to :user
end
However when I open script/console and try to access the users starbases it gives me an error: NameError: uninitialized constant User::Starbasis.
It seems as if it is a problem with inflection and rails is not pluralizing starbase correct.
I have tried adding this to the inflections.rb in the intializers folder:
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural 'starbase', 'starbases'
end
but it still does not solve the issue. Could anyone give advice on how to get this working?
Have you tried adding a line for the inverse inflection (i.e. 'singular'):
inflect.singular "starbases", "starbase"
I tried your example in my console and it was the singularization that caused problems, not the other way around. I'm not sure if this fixes other issues (like routes), but it should fix the simple stuff (I think).
Little trick i picked up to double check how Active Support might singularize, or pluralize my Class names, and/or Module names.
have your rails app server running and in a new tab enter into your rails console by typing rails console. In there you can easily double check for the correct style for your names.
long way ActiveSupport::Inflector.pluralize "fish"
# => "fish"
short way "fish".pluralize
# => "fish"
You can find more examples here
https://github.com/rails/rails/blob/master/activesupport/test/inflector_test_cases.rb
Related
I keep getting this error:
NameError (uninitialized constant Character::Messagemissife):
uninitialized constant Mime::HTML
The error is coming from this line:
if #character.messagemissives
character.rb
has_many :messagemissives, dependent: :destroy
messagemissive.rb
class Messagemissive < Missive
self.table_name = 'messagemissives'
belongs_to :character
end
missive.rb
class Missive < ActiveRecord::Base
self.abstract_class = true
end
I have a class Messagemissive, but not Messagemissife. Of course, it looks like a typo error. But I can't find "Messagemissife" anywhere in any of my files. I've used the Find function in Sublime Text 2, I've used the mac Finder search, I've cleared the cache, I've restarted the server several times, I've restarted the computer several times. Still this error won't go away. What am I doing wrong?
You are seeing this behaviour because of rails's default naming convention. When you call #character.messagemissives, rails is actually looking for a model with it's corresponding singular term Messagemissife and not Messagemissive. You can confirm this by typing"Messagemissives".singularize
in rails console which will return you "Messagemissife".
To fix this issue, either you can mention the class name with association like
has_many :messagemissives, class_name: 'Messagemissive'
or as mentioned here, in /config/initializers/inflections.rb, just add
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'messagemissive', 'messagemissives'
end
Hope this will help.
I'm trying to manually add data to a table in rails using the rails console, but I keep getting a undefined local variable or method error.
The model is namespaced under ratings:
models/ratings/value_for_money_score.rb
class Ratings::ValueForMoneyScore < ApplicationRecord
end
To try and manually add a record to the database, I run the following in the console:
$ RatingsValueForMoneyScore.create(score: '1', description:"Terrible, definitely not worth what was paid!")
And I get this error: NameError: uninitialized constant RatingsValueForMoneyScore
I have tried a few different versions such as
RatingsValueForMoneyScores.create,
Ratings_ValueForMoneyScores.create,
ratings_value_for_money_scores.create but keep getting the same error. What am I doing wrong?
Try
::ValueForMoneyScore.create(score: '1', description:"Terrible, definitely not worth what was paid!")
The error is descriptive enough in this case, the class RatingsValueForMoneyScore pretty much doesn't exist. Check your namespace, you have a class name (which should be singular) Rating, and the module ValueForMoneyScore. You'd use either
(after renaming the class to Rating)
Rating.create(...)
or
ValueForMoneyScores.create(...)
Your syntax is equivalent to:
class Rating
module ValueForMoneyScores < ApplicationRecord
...
end
end
The answer was a combination of input, so collecting that in one answer.
My namespaces weren't properly set up (I was using "Ratings" rather than "Rating"), so I fixed this for the tables. My models then looked as follows:
models/rating.rb
class Rating < ApplicationRecord
end
models/rating/value_for_money_score.rb
class Rating::ValueForMoneyScore < ApplicationRecord
end
And then this command worked for creating records in the rating_value_for_money_score table
$ Rating::ValueForMoneyScore.create(score: '1', description: "Test")
I have a model named Fave. It belongs to another model named User.
I'm trying to find a User's Faves by calling #user.faves. My server gives me back the following error:
NameError: uninitialized constant User::Fafe
Why would it think the singular of "faves" is "fafe"? Is there some other plural form I can use that will point to "fave"?
You can pass the class name while setting up the association
has_many :faves, class_name: "Fave"
Can we try this in config/initializers/inflections.rb? This could work
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'fave', 'faves' #append this to the existing ones
end
I have an Application model in a Rails 4 app.
It is giving me some strange errors in tests, including
NoMethodError: undefined method `user_id=' for #<Application:0x007f851222d370>
and
ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
The model definitely has a user_id column. The migration looks like this:
....
t.references :user, index: true, foreign_key: true
...
and inspecting Application.column_names in the console reveals it to be there.
application.rb and user.rb both have the relevant belongs_to and has_many calls defined.
I'm scratching my head and the only thing I can think of is that the term Application behaves strangely in Rails. Is this the case? Or have I missed something obvious?
Rails does not declare Application in the "top-level namespace". There is Rails.application and rails generates a ApplicationController by default.
However you will need to often need to explicitly use ::Application to avoid confusion with Rails::Application.
You don't even have to follow the Rails convention of extending ApplicationController.
However that said having a model named Application may be a bad idea since any poor sod who later has to work on your code will be very confused.
It's a conflict with Rails::Application class or its subclass, defined in config/application.rb, I believe.
I have a have one relationship between two models. The relationship works, the problem however is that when I write a Rspec test with shoulda matchers I get returned an error.
Failure/Error: it { should have_one(:userresetpassword)}
NameError:
uninitialized constant User::Userresetpassword
My code in the user.rb
has_one :userresetpassword
My rspec test
describe "associations" do
it { should have_one(:userresetpassword)}
end
Your association is mis-named, and either rails, shoulda-matchers or both are having a hard time guessing the class name from it.
You have two choices. First, and preferable is to rename the association to be conventional:
has_one :user_reset_password
This will allow rails to correctly guess the classname UserResetPassword
Second, you could simply remove the guesswork and tell rails what the classname is. This is only preferable if you can't or very much do not want to change the association name.
has_one :userresetpassword, :class_name => "UserResetPassword"