what's wrong in this seed.rb?

lines = FasterCSV.read(Rails.root.join('public', 'data', 'negozi.csv'))
lines.slice!(0)
lines.each do |row|
  if row.any?
    begin
      role_date = Date.strptime("#{row[4]}", "%d/%m/%Y")
      reference_date = Date.strptime("#{row[6]}", "%d/%m/%Y")
    rescue
      reference_date = role_date = nil # se la data non e' valida
inserisci un valore nullo
    end
    shop = Shop.create!(:role_number => row[3], :role_date =>
role_date, :name => row[7], :ssn => row[8],
                :nationality => row[9], :address => "#{row[10]}
#{row[11]}", :street_number => row[12],
                :website => row[24], :district => row[13], :sector => row[15],
                :square_meters => row[16], :state => row[2],
                :notes => row[22])
    shop.documents.create!(:reference_number => row[5],
:reference_date => reference_date,
                :subject => row[14])

rake db:seed
Then I open console: rails c and I do
shop=Shop.where(:role_number => '5')
shop.documents
I have method missing documents error.
Why?

Shop has_many :documents and Document belongs_to :shop

Are you sure that Shop.where(:role_number => ‘5’) is returning only one record?

What’s the output of this:

puts Shop.where(:role_number => ‘5’).class

shop=Shop.where(:role_number => '5')
shop.documents
I have method missing documents error.
Why?

Are you sure that Shop.where(:role_number => '5') is returning only one
record?

What's the output of this:

puts Shop.where(:role_number => '5').class

ActiveRecord::Relation

Actually, it doesn’t matter. I just rememebred where() is always going to return and ActiveRecord::Relation instance, not an instance of your model.

You’ll need to access one of the model instances to be able to see the documents:

shops = Shop.where(:role_number => ‘5’)
shops[0].documents

Ah ok.
shop=Shop.find_by_role_number(5)

now shop.documents works.
Thank you.