Problem With Active Record. Associations Showing Error ..ple

i am stuck somewhere with The Acrive record association .
whenever i try to get the value from another table it gives me the
foolowing error
"undefined method `journal_name' for nil:NilClass"

MY Controller

def index
@ledgers = Ledger.find(:all)
@finaccounts = Finaccount.find(:all)
@journels = Journel.all
respond_to do |format|
format.html
format.pdf { render :layout => false }
end

view:

    <td><%=h ledger.journel.journal_name %></td>

Models :
1) Journel

class Journel < ActiveRecord::Base
has_many :ledgers
end

2)

class Ledger < ActiveRecord::Base
belongs_to :journel
end

Apparently one or more of your ledgers doesn't have a journel?

Try:
<td><%=h ledger.journel.journal_name if ledger.journel %></td>
to see the ones that don't have journels.

Thanks For the Help Heinz Strunk.
But i need To Know Is there anything with rails 2.3.5 because to check
wheather i have made any mistake in my application i created a sample
application , and again it was the same error i got .
waiting for your reply
and one more thing ..i am only a couple of months old to Ruby On rails
.so please if possible suggest me how to make the associations between
tables in rails.

If you are much interested in Rails Associations means, refer these
URLs.

http://guides.rubyonrails.org/association_basics.html
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Sorry But I guess I have read all The Associations ..the only thing i
want to know is there any problem with rails 2.3.5 because when i make
the associations in 2.3.5 i get the same error
undefined method `some method' for nil:NilClass"

Veera Sundaravel wrote:

Sorry But I guess I have read all The Associations ..the only thing i
want to know is there any problem with rails 2.3.5 because when i make
the associations in 2.3.5 i get the same error
undefined method `some method' for nil:NilClass"

Unlikely. As Heinz said, the most likely explanation is that some of
your ledgers don't have a journel.

Fred

Sorry But I guess I have read all The Associations …the only thing i
want to know is there any problem with rails 2.3.5 because when i make

the associations in 2.3.5 i get the same error
undefined method `some method’ for nil:NilClass"

You can use “rails console” to start up the RAILS console and look up the values of various objects and make a decision why you are getting null. Most probable reason why you are getting null is that “journel_id” value is not set in Ledger records.

In RAILS console, do:

Ledger.first

…observe the field values…, look wheher journel_id has valid integer

Ledger.first.journel

…if you get null, then, journel_id is not correct. try to set it to a valid jourenl

lg = Ledger.first

lg.journel = Journel.first

lg.save!

Ledger.first.jourenl

…now you should not see null…

I doubt there's a problem with Rails 2.3.5

Fire up the console and try following:
ledger = Ledger.first
ledger.journel = Journel.first
ledger.journel.journal_name

This should work and if so the ledgers you're calling in the view just
doesn't have a journel. If not something's wrong with the whole thing :slight_smile: