0

There has been several times answers to some questions online show the use of inline lambda -> { } as a value for default in a ruby hash which is given as an optional argument to a method. An example of this is add_timestamps :model, null: false, default: -> { 'NOW()' } or opt :color, cast: "bool", default: -> { true }.

I attempted to hunt for this in google, Ruby on Rails Guides but I most likely am not searching the right terms and am not able to find any answers about this.

Edit:

Just want to clarify that I am not looking a specific answer on how to set the default value in the DB to NOW(). I am looking for the theory of when a lambda is the correct tool to use when setting a default value.

Some posts that I have reviewed which did not have the answer:

When to use a lambda in Ruby on Rails?

In a Rails migraiton, how do I set the default value of a column to be NOW() instead of the time when I ran the migration?

Frostyfx
  • 1
  • 2
  • Based on recent questions, you seem to be interested in `default: ->{...}` with respect to migrations. Hopefully the duplicate clears things up. – mu is too short Jan 25 '23 at 01:35
  • I am not sure what you mean by "the duplicate clears things up". Is there a question that was asked which already provides the answer to this question which I did not find. If so, could you please provide a link to this. – Frostyfx Jan 25 '23 at 15:09
  • I marked this as a duplicate of another question about `default: -> { 'now()' }` in migrations, there's a link to the duplicate at the top of your question. – mu is too short Jan 25 '23 at 18:32
  • Thank you, I reviewed the post that was linked and it does not address my question. I have added some more verbiage to the original post to add clarification. – Frostyfx Jan 30 '23 at 17:36
  • Rule of thumb: if you don't want Rails to try to interpret the default in any way and just pass something straight to the database (such as `default now()`), yes a lambda. Sorry, I don't remember how I discovered this and I can't find any decent documentation on it. – mu is too short Jan 31 '23 at 03:45

0 Answers0