I have a Blog model of which content will be stored in a file and the remaining things will be stored in the table.
My solution for this is as below
I created a virtual attribute with getter/setter method.
def content
File.read(file_path)
end
def content=(html)
File.open(file_path, "w") do |file|
file.write(html)
end
end
But here I have a problem, I want to store html content to a file only if the record is valid, but when I say something like below in the controller
Blog.new(:content => html,:title => "Title".......)
My setter is called first before other attributes are set. Since other attributes are validated with presence true record will fail to save but still my file will be stored.
So now I want a solution where file will be created only if the record is valid and it is stored.