开发者

String to Serialized Array?

开发者 https://www.devze.com 2023-02-04 20:33 出处:网络
I\'m sure thi开发者_JS百科s 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\"

I'm sure thi开发者_JS百科s 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 < ActiveRecord::Base
  serialize :keywords, Array
  attr_accessible :keywords

  before_validation :update_keywords

  def update_keywords
    self.update_attributes(:keywords, self.keywords.split(',').collect(&:strip)
  end
end

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.


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.


Try like this

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号