Rails thinks "faves" is referring to a model named "Fafe" - ruby-on-rails

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

Related

Manually adding data to a namespaced model in rails

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")

Rails Has One Rspec Test tells me Unitialized Constant

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"

undefined local variable or method `new_media_path' - resources to resource

I have odd problem:
After starting server I got this error:
undefined local variable or method `new_media_path'
To repair this i must go to routes.rb and change
resources :media
to
resource :media
and again to
resources :media
It's annoying. Any ideas to solve this?
You should try new_medium_path because media is plural form of medium
If you run rake routes you will see all available routes.
You can also inform rails about the proper pluralization using the Inflector class. It handles most works fine, but non-standard pluralizations like 'media' aren't always pre-defined. To add your own, edit config/initializers/inflections.rb, and add this at the end:
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'medium', 'media'
end
This should let Rails handle all the plural/singular stuff - note this will affect that it thinks DB table names will be as well, so it'll expect the model to be class Medium, and the table name will be media
To turn the plural and singular to the same thing (i.e. always 'media'), use:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable 'media'
end

Rails model singularize not working correctly

I have a Mongoid model called "GradebookSettings". I've gone into inflections and added:
inflect.singular("GradebookSettings", "GradebookSettings")
When I go into irb, it properly singularizes "GradebookSettings" to "GradebookSettings". However, when I try to access an associated model, it keeps trying to singularize it to GradebookSetting.
I am using Mongoid. I am tempted to pluralize GradebookSettings with two s' but I'd rather not.
Thanks!
You don't need to trick the inflector. Use the :class_name option of the association to set the class instead:
embeds_many :gbsettings, :class_name => "GradebookSettings"

Ruby on Rails Inflection error - uninitialized constant

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

Resources