I am using virtual attributes to break out a comma separated parameter list in the view, and then trying to recombine them to save into my Active Record model.
Given the real attribute “ad_columns” that defaults to “1,1,1,1”
I am breaking them out in to individual attributes for the form:
attr_accessor :top_rows
#Getter
def top_rows
split = ad_rows.split(',', 4)
split[0]
end
#Setter
def top_rows=(trows)
ad_rows_will_change!
self.ad_rows = [trows, self.right_rows, self.bottom_rows, self.left_rows].join(",")
end
Then repeating this for right, bottom, left and right.
Given an object if I call “object.top_rows” I do get “1”, and if I update it in irb:
object.top_rows = "3"
Then it updates the ad_rows real attribute properly. I can see that the object has changes, and when I do an
object.save
The changes are updated in the database.
The problem is, this is NOT working from the view. It will not save to the database. I have even used logger.info to see if the model has changed and it will show that “ad_rows” has indeed been changed, yet active record is still NOT updating the real attribute.
I can’t figure out why this is happening. Am I just doing it wrong? 🙂 Thanks.
Why would it work in irb but not the view?
It seems that since the attribute :ad_rows was not being passed in the parameters, it was not being recognized as changed by the controller.
I had to add:
object.ad_rows_will_change!
To my controller to force it to save the ad_rows column.
I am not sure if this is the best solution, but it is working for now.