String to Serialized Array?

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

I'm sure this is trivial, but I've been banging my head against the desk for the last few hours. I'm trying to convert a string (i.e. "key1,key2:) to an array (i.e. ["key1","key2"]) before storing it in the database. I'm using a before_validation callback in my model and it doesn't seem to be firing.

My model looks like this:

class Product 

I'm getting a SerializationTypeMismatch error. Obviously, either the update_keywords method isn't being run or isn't properly returning the updated attributes.

Any ideas?

EDIT
I'm using Rails 3.0.3, if that makes any difference.

EDIT #2
Just wanted to follow up and say that I've found that removing the serialized columns type declaration and ensuring that it defaults to an empty array (i.e. []) rather than nil clears up many issues.

For the sake of anyone like myself just beginning their journey with Rails, I should note that this most likely not the best way to go about creating serialized attributes. I'm just porting over a project that utilizes an old database.

回答1:

Change the implementation for update_keywords as follows:

def update_keywords   if keywords_changed? and keywords.is_a?(String)     self.keywords = keywords.split(',').collect(&:strip)    end end 

The update_attributes updates the DB attribute NOT the object attribute. In order to assign a value to an object attribute use the assignment operator.

product.name = "Camping Gear" product.keywords = "camping, sports"  product.save #  ----  #  - "update_attributes" updates the table #  - "save" persists current state of the object(with `keywords` set to string.) #  - "save" fails as `keywords` is not an array #  --- 

In the solution, the changed? check makes sure that the array conversion code is run only when the keywords value changes.



回答2:

Try like this

def update_keywords   self.keywords = self.keywords.split(",").map(&:strip) if self.keywords.is_a?(String) end 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!