Can't dup NilClass on association methods
Got it.
From here,
Some of the classes inherited or
included in your engine controllers
may fail to get unloaded and cause
trouble after the first request to
your system.
For me, it was because I had a file in the lib
that was monkey patching User model and the
User model class in this file was not getting cached I suppose.
Calling unloadable
in that class in the lib folder did the trick. So my lib file looks like this
class User < ActiveRecord::Base
unloadable
# stuff...
end
Thanks anyways.
can't dup NilClass - Error
Why are you marking the Child as unloadable? Is there a good reason for this? if not, I'd remove.
Rails API says "Unloadable constants are removed each time dependencies are cleared."
Does the error happen when you change it to:
class Child < Parent
has_many :parents, :foreign_key => "child"
end
And, I may be overstepping, but this seems more standard:
class Child
belongs_to :parent
end
class Parent
has_many :children, :dependent=>:destroy
end
Active record destroy give me Can't dup NilClass
Is it possibly somehow confusing your Node class with this one?
http://apidock.com/ruby/REXML/Node
can't dup NilClass when using Sanitize gem
This will happen if you pass nil
into clean()
instead of a string. Make sure your string
variable is really a string.
Rails can't dup NilClass error on params
My mistake. I apologize profusely for this, but I was using paginate inappropriately. I wish I could delete this question, but there doesn't seem to be a way. The proper way to use it is:
paginate(:page => params[:page]
I guess that's what happens when you've been staring at code for too long :)
Maybe it will help others deal with Paginate. I really like it, but the errors could really be a little more descriptive.
For example, I will always get an unknown method error unless I do this:
<%= will_paginate @artists if @artists.respond_to? :total_pages %>
undefined method `username' for nil:NilClass when adding has_many through association
This is your problem in the User model...
has_many :goals, dependent: :destroy
has_many :participations, dependent: :destroy
has_many :goals, through: :participations, source: :goal
You've defined has_many :goals
twoice, so the line
@goal = current_user.goals.build(goal_params)
Doesn't build the @goal
object correctly.
Try this instead...
has_many :goals, dependent: :destroy
has_many :participations, dependent: :destroy
has_many :participants_goals, through: :participations, class_name: 'Goal'
How to add a has_many association on all models
Thanks to Rich Kilmer (of InfoEther), we found the elegant (and slightly opaque) way of fixing this:
# config/initializers/has_many_notes.rb
module ActiveRecord
class Base
def self.inherited(klass)
super
klass.send :has_many, :notes, :as => :notable
klass.send :accepts_nested_attributes_for, :notes
end
end
end
Now no inheritance changes, and it's very DRY
Related Topics
How to Split String into 2 Parts After Certain Position
No Such File or Directory @ Rb_Sysopen for External Url/Rails 6.11/Ruby 3
Why Does Ruby Use Its Own Syntax for Safe Navigation Operator
Rails: Organizing Models in Subfolders Having Warning: Toplevel Constant a Referenced by B::A
Uri::Invalidurierror: Bad Uri(Is Not Uri) Testing Rails Controllers
What Does "File.Sync = True" Do
Ruby - Convert Integer to String
How to Stop Bundler from Adding Ruby Version to Gemfile.Lock
Can You Have Multiple Versions of a Gem in a Gemfile
How to Interpolate a Variable in a Ruby Regex
There Is a Way to Handle 'After_Save' and 'After_Destroy' "Equally"
Quote All Fields in CSV Output
When Should I Use Datetime VS Date, Time Fields in Ruby/Rails
Why Do We Need Nginx with Thin on Production Setup
Split Float into Integer and Decimals in Ruby
Return True Only If All Values Evaluate to True in Ruby