Reputation: 1878
I have a DI routine where I have a large csv I'm importing with known column format. I first set up a column map:
col_map =
{
4 => :name,
6 => :description,
21 => :in_stock,
...
I then read each line in, and then using the column map, attempt to set the attribute:
i = Item.new
col_map.each do |k,v|
i[v] = chunks[k] #chunks is the line read in split by the delimiter
In my item declaration, I declare two attributes, b/c these are not stored in the database, they're used for other logic:
attr_writer :in_stock
attr_writer :end_date
When the code gets to this line:
i[v] = chunks[k]
I get this message:
X.DEPRECATION WARNING: You're trying to create an attribute `in_stock'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer`
But I'm not trying to create an attribute, and I am using attr_writer. I suspect this has something to do with the [] I'm using instead of . for the lvalue.
Can anyone see what I'm doing wrong?
Thanks for any help, Kevin
Upvotes: 1
Views: 187
Reputation: 66263
Admittedly, the deprecation wording is slightly confusing, but you're seeing this warning because the model[attribute_name] = ...
style is only supported for ActiveRecord attributes on the model, not non-persisted attributes added with attr_writer
.
You can see the code that produces the warning over here.
To address this I'd use send
which will work for all attributes e.g.
i.send("#{v}=", chunks[k])
Upvotes: 2