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?
msan
2
Shop has_many :documents and Document belongs_to :shop
msan
3
On Thursday, May 5, 2011 3:19:33 PM UTC-4, mauro wrote:
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
msan
4
On 5 May 2011 21:25, Tim S. [email protected] wrote:
What’s the output of this:
puts Shop.where(:role_number => ‘5’).class
ActiveRecord::Relation
msan
5
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
msan
6
On 5 May 2011 21:28, Tim S. [email protected] wrote:
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.