开发者

how to assign different value to a column for different rows

For example, there is a table like following:

Column_A Column_B Column_C

  1. I want to add a column D using ruby migration
  2. I have a function getColumnDValue(columnC). This function can get the value of column D based on the column C value
  3. I want to assig开发者_如何学Pythonn different values to column D based on the column C value.

How to do these????

I've checked the Update_All and Update. However, it seems they are not useful for my case. Or at least, I don't know how to use these methods to achieve my purpose.


If I understand you correctly, you want to be able to set the value of d the usual way, like so: myobject.d = 5. Based on the value in c, d will be saved with the correct value.

This could be accomplished with a before_save filter, where you set a new value for d.

You could also overwrite the setter method for d:

def d=(value)
  write_attribute(:d, getColumnDValue(value))
end

With the first method, the filter, your object will carry the "invalid" value until saved. If you overwrite the setter, the new value will be written to the object immediately. It's up to you to decide which method fits you best.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜