'Mobility accessing values in rake task
A looked up class
extend Mobility
translates :name, type: :string
is invoked in the following block of a CSV import function
Mobility.with_locale(:en) do
@regionmajor = Regionmajor.where(['nation_id = ? AND name = ?', row[0], row[1].to_s]).first
@regionminor = Regionminor.where(['nation_id = ? AND name = ?', row[0], row[2].to_s]).first
end
Municipal.create(
nation_id: row[0],
regionmajor_id: @regionmajor.id,
regionminor_id: @regionminor.id, [...]
)
is not fuctioning as the object found is nil:
Error importing row because 'undefined method `id' for nil:NilClass
regionmajor_id: @regionmajor.id,
attempting to query Regionmajor.where(['nation_id = ? AND name_en = ?', row[0], row[1].to_s]).first complains that the field is. logically so, inexistent.
Because of the mechanics of Mobility, in a key/value implementation, the name field is effectively nil.
How can this query be effectively be written so as to access the name_en value
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
