Hello,
in my database i have tables models,marques,cars. I wrote fixtures for
models f1:
id: 1
marque_id: 1
name: 100 NX
f2:
id: 2
marque_id: 1
name: 200 SX
f3:
id: 3
marque_id: 1
name: Almera
f4:
id: 4
marque_id: 1
name: Patrol
f5:
id: 5
marque_id: 1
name: Primera
f6:
id: 6
marque_id: 2
name: CLK
f7:
id: 7
marque_id: 2
name: CLS
f8:
id: 8
marque_id: 2
name: R
f9:
id: 9
marque_id: 3
name: 400
f10:
id: 10
marque_id:
name: 500
f11:
id: 11
marque_id: 4
name: 159
f12:
id: 12
marque_id: 4
name: 164
f13:
id: 13
marque_id: 4
name: 163
f14:
id: 14
marque_id: 5
name: V70
f15:
id: 15
marque_id: 6
name: Jeep Cherokee
f16:
id: 16
marque_id: 7
name: 100
f17:
id: 17
marque_id: 7
name: A8
f18:
id: 18
marque_id: 8
name: 323
f19:
id: 19
marque_id: 8
name: 929
f20:
id: 20
marque_id: 9
name: Avensis
f21:
id: 21
marque_id: 9
name: Highlander
f22:
id: 22
marque_id: 9
name: Vista
and for marques
f1:
id: 1
name: Nissan
f2:
id: 2
name: Mercedes-Benz
f3:
id: 3
name: Aixam
f4:
id: 4
name: Alfa Romeo
f5:
id: 5
name: Volvo
f6:
id: 6
name: Chrysler
f7:
id: 7
name: Audi
f8:
id: 8
name: Mazda
f9:
id: 9
name: Toyota
So now i would like to generate fixture for table cars.
what I do is
<% marques = Marque.find_all %>
<% models = Array.new %>
<% Model.find_all.each do |model| %>
models[model.marque_id]<<model
<% end %>
<% 0.upto(10) do |i| %>
<% marque_i = rand marques.length %>
car<%= i %>:
marque_id: <%= marques[marque_i].id %>
model_id: <%= models[marques[marque_i].id].id %>
<% end %>
But it looks like Marque.find_all doesn''t return anything because doing
"marques[marque_i].id" makes error. It says, that I can not access id
of
object nil.
My fixture loading order is defined so that first models and marques
fixtures are loaded, then fixture "cars.yml".
Please, help me to make <% marques = Marque.find_all %> work . Or it is
not possible to make database calls from fixtures ?
Thank you
--
Posted via http://www.ruby-forum.com/.