but i am getting error: undefined method `build’ for nil:NilClass
If you look on the line in your code where the error occurs you will
likely find that you are using something.build. The error is
indicating the your ‘something’ is nil.
If you cannot see why it is nil it might be worth your while looking
at the rails guide on debugging at http://guides.rubyonrails.org.
This shows various methods of debugging your code. For example you
can use ruby-debug to break into your code and inspect your data to
see what the problem is.
hello colin,
thanks for your reply,
i know what that error means… but my problem is why this is comming
& how can i achieve my solution.
Did you try my suggestion to use ruby-debug?
more details:
content_master model:
class ContentMaster < ActiveRecord::Base
has_one :Metadata
That should be :metadata not :Metadata
end
metadata model:
class Metadata < ActiveRecord::Base
belongs_to :ContentMaster, :class_name => ‘ContentMaster’
That should be :content_master and then the class name will not be
needed
end
new action in controller
def new @content_master = ContentMaster.new @metadata = @content_master.Metadata.build
Should be metadata again.
i hope now my problem is more clear
It might be worth your while having a look at the ActiveRecord
Associations guide aswell as the debugging one. That will explain how
to use associations.
Did you try my suggestion to use ruby-debug?
i havent did that yet will do it as soon as possible and will report
you back
def new @content_master = ContentMaster.new @metadata = @content_master.Metadata.build
Should be metadata again.
though m beginner in Ruby as well as rails but i have read on some
blog that rails just convert every simple simple and underscore words
to camelCase( where required ) so it hardly matters how you write it,
they are just conventions. Though i know it is good practise to
follow conventions but i dont think it makes any error.
will do ruby debug ( for now i even dont now how to do this ) , and
will post back about result or if i found something fruitful their
I would very strongly suggest following the conventions, it will make
your life much easier. Also I think that in this case it may at least
in part be the cause of the problem. Consider the line @metadata = @content_master.Metadata.build
What is Metadata here? In the model you have declared
class Metadata < ActiveRecord::Base
so Metadata is a class. But in the code you are using it as if it
were a member of the ContentMaster class, which it is not, whereas
metadata is (or at least could be, dependent on the table schema).
I suggest that you make the changes I have proposed and try it.
i have tried all the conventions you told but it does no change in
error ( hard luck ).
I suppose their is lots and lots of chaos about rails conventions &
defaults.
end
3) new action in controller
to camelCase( where required ) so it hardly matters how you write it,
they are just conventions. Though i know it is good practise to
follow conventions but i dont think it makes any error.
I would very strongly suggest following the conventions, it will make
your life much easier. Also I think that in this case it may at least
in part be the cause of the problem. Consider the line @metadata = @content_master.Metadata.build
What is Metadata here? In the model you have declared
class Metadata < ActiveRecord::Base
so Metadata is a class. But in the code you are using it as if it
were a member of the ContentMaster class, which it is not, whereas
metadata is (or at least could be, dependent on the table schema).
I suggest that you make the changes I have proposed and try it.
def new @content_master = ContentMaster.new @content_master.metadata = Metadata.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @content_master }
end
end
I like to leave this to the model where possible - if I have to have
an associated object (or if I don’t want to do loads of guards like
“@content_master.metadata.attribute unless @content_master.metadata.nil?”) I build/create the associated object
after initialise.
#content_master model
def after_initialize
metadata ||= Metadata.new
end
… it can cause problems in some cases, but can solve some in others!
(a bit of advice gleaned from the “Refactoring: Ruby Edition” book)