0

EDIT: Solved, I had to change the serialized column from type integer to type text because int doesn't work with serialize.

Is there some catch to saving to the database that I'm missing? I'm trying to save an array to a database column. I am using serialize so that I can store multiple team IDs in a column. It works if I do it in rails console, but doesn't seem to work when I do it from my model.

Here's what works in Rails console:

match_entry = Match.new
match_entry.team_ids << 1
match_entry.team_ids << 2
match_entry.save

After it saves, I see that match_entry.team_ids contains [1, 2]. Here's what I have in my model that doesn't work:

file.each_line do |line|
    if line.include?("team_ids")
        str = line.sub("team_ids:", "").chomp
        arr = str.split(",")
        match_entry.team_ids << arr[0].to_i
        match_entry.team_ids << arr[1].to_i
    end
end
match_entry.save

match_entry.team_ids even returns [1, 2] correctly, but it doesn't seem to actually save to the database. It'll save to the database with no value for team_ids.

2
  • What version of rails? Feb 20, 2015 at 8:18
  • @FrederickCheung, It's Rails 4.2.0 rc1
    – Gary M
    Feb 20, 2015 at 17:51

2 Answers 2

0

It's hard to tell from your code snippet in the model if what you're doing is being run on an instance of the object Match or on the Class Match which then creates a new Match object in which to save.

Regardless of how you do it, I think your issue is that the instance object of Match you are manipulating in the model is not being saved after manipulation. Your code block should work as written if called from a before_save callback.

class Match
 before_save :add_team_ids

private
 def add_team_ids
  self.team_ids << 1
  self.team_ids << 2
 end
end

In an after_save callback you would need to save again since the object was first saved before the after_save callback fired.

class Match
 after_save :add_team_ids

private
 def add_team_ids
  self.team_ids << 1
  self.team_ids << 2
  self.save
 end
end
4
  • I think you may be right about it. I've got a class named Match and within one of its methods, I did match_entry = Match.new. I think that may be causing the problem and I'll look into it, thanks!
    – Gary M
    Feb 20, 2015 at 3:49
  • You're welcome. You should just need to do a match_entry.save in that method to get the new object to save. Feb 20, 2015 at 3:52
  • Interesting. I already do call match_entry.save in the method that I call match_entry.new in, but it doesn't seem to work. I'm unable to try some things right now, but perhaps later tonight.
    – Gary M
    Feb 20, 2015 at 3:54
  • Before I changed anything, I tried to set values for non-serialized columns and they worked perfectly fine. It only didn't work when I tried it on the serialized columns, it seems.
    – Gary M
    Feb 20, 2015 at 17:55
0

I found out that serialize doesn't work with integers, so I had to change the column to be of type text for it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.