I have a namespaced model Collection::Publication with a has_many relationship to another namespaced model Library::Publication. The association declaration looks like this:
has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
Yet when I try something like this:
#collection_publication.library_publications
I get this error:
NameError at /
uninitialized constant Collection::Publication::Library::Publication
It seems that Rails is namespacing the associated model, even when I explicitly define the class_name. Am I understanding this correctly? And if so, what can I do to remedy the situation?
This was due to a typo in the Library::Publication declaration:
class Libary::Publication
...
end
Although the error message declared that it wasn't finding Collection::Publication::Library::Publication, this must have been only the last place it looked. Once Library::Publication was properly defined, Rails found it without problem.
Related
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 situation where an assignment has one training session associated with it
class Assignment < ActiveRecord::Base
has_one :trainingsession
end
class TrainingSession < ActiveRecord::Base
belongs_to :assignment
has_many :drills
end
I keep getting a uninitialized constant error when I'm trying to build an object with a has_one relationship
I'm using the following to build the training session in my controller
#activetrainingsession = #assignment.build_trainingsession
And that line blows up with the uninitialized constant
Something that seems like it should be straightforward!!
By convention, Rails uses camelize and underscore to switch between camel case and underscored representations. This means, in your case, that TrainingSession would be properly referenced as training_session (not trainingsession).
You need:
#activetrainingsession = #assignment.build_training_session
But, to follow said convention all the way through, it may be better as:
#active_training_session = #assignment.build_training_session
I'm trying to do a pretty simple join in my model to list all 'Locations' in a 'Post' with a certain id.
Currently, each post has_many :locations, :through => :location_post. I'm using the 'blogit' gem, which puts posts in a module named 'Blogit::Posts'.
I'm getting a wrong argument type Class (expected Module) error when I try to run the following in my Post.rb model:
module Blogit
class Post < ActiveRecord::Base
before_save :reuse_existing_locations
def reuse_existing_locations
existing_locations = Location.include(Blogit::Post).first
end
How can I do a join through a module?
Thanks in advance!
I'm not sure I understand what you're trying to accomplish so just some notes and observations:
By looking at the code, it's clear that Blogit::Post is a class, not a module.
The include method takes modules (not classes), that's the error you're seeing.
You are calling the include method on the Location model and that seems kind
of strange to me. Did you mean to call includes? But then again that
wouldn't make much sense since it seems like you've got a many to many
relationship between Location and Blogit::Post.
In the Location model (which doesn't need to be in the Blogit namespace), you can simply reference the Blogit::Post model as
follows:
has_many :posts, class_name: "Blogit::Post", ...
If existing_locations is in fact an attribute on the model and you want to assign to it, you need to put self in front of it (as in self.existing_locations). Otherwise you're just creating a local variable.
You probably wanted to use ActiveModels includes instead of Rubys include, which is to include methods from another module.
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"
I have a model called produccion_alternativa.
I added a new inflection rule in config/initializers/inflections.rb, like this:
inflect.irregular('produccion_alternativa', 'producciones_alternativas')
I have other model called productor that has a relation with produccion_alternativa:
class Productor < ActiveRecord::Base
has_many :producciones_alternativas
class ProduccionAlternativa < ActiveRecord::Base
belongs_to :productor
When I try to get all the producciones_alternativas for a productor, I get this error:
irb(main):010:0> Productor.first.producciones_alternativas
NameError: uninitialized constant Productor::ProduccionesAlternativa
Any ideas?
I see several others having the same problem. Couldn't find an answer why this happens. So in the meantime you could just try this:
has_many :producciones_alternativas, :class_name => "ProduccionAlternativa"
Your Fail is that you pluralized both words in has_many association. You used:
has_many :producciones_alternativas
but based on the class name ProduccionAlternativa the plural is produccion_alternativas because only the last word is pluralized! So this should work:
has_many :produccion_alternativas
To check the Plural of a word type "your_word".pluralize in the rails console!
I found another solution too. I added another rule on inflection.rb:
inflect.irregular('ProduccionAlternativa', 'ProduccionesAlternativas')
inflect.irregular('produccion_alternativa', 'producciones_alternativas')
At least, now it's working as I want.