1

Hai i want to know how to set a default value from the database if my params is nil

I am trying to update a record in my database I have used this code

  if @sheet = Sheet.where(id: sheet_id).first_or_initialize
    @sheet.user_id = user_id
    @sheet.name = sheet_name ## I want to set a default value for this
    @sheet.save
  end

I want to set the default value of sheet name from database if my params[sheet_name] is nil.

Update This is the thing i am trying to achieve

if sheet_name != nil
  @sheet.name = sheet_name
end

is there any short hand way of writing this

Amal T S
  • 47
  • 7
  • What value would be that? A "dynamic" one, or always the same value? – Sebastián Palma Aug 18 '19 at 10:15
  • @SebastianPalma i need to set the value which is currently in database if params[sheet_name] is null – Amal T S Aug 18 '19 at 10:45
  • @spickermann i am using a method for both updation and creation. For example if i give a value into params[sheet_name] = Sheet 1 it should set the name to Sheet 1. If i try to update that record if no params[sheet_name] is given it should set the default value which is Sheet 1. – Amal T S Aug 18 '19 at 10:48

2 Answers2

1

In your example this should work:

if @sheet = Sheet.where(id: sheet_id).first_or_initialize
  @sheet.user_id = user_id
  @sheet.name = sheet_name.presence || 'Sheet 1'
  @sheet.save
end

Depending on your use-case you might want to consider setting the default in the database.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • I am using a method for both updation and creation. For example if i give a value into params[sheet_name] = Sheet 1 it should set the name to Sheet 1. If i try to update that record if no params[sheet_name] is given it should set the default value which is Sheet 1. – Amal T S Aug 18 '19 at 10:49
  • Thank you for answering but please check the comment. – Amal T S Aug 18 '19 at 10:49
  • @AmalTS I still think my answer should solve your question. If it does not please elaborate on what is going wrong or is missing. – spickermann Aug 18 '19 at 12:04
  • What does the sheet_name.presence does ? – Amal T S Aug 18 '19 at 12:50
  • It calls the [`presence` method](https://api.rubyonrails.org/classes/Object.html#method-i-presence) on `sheet_name` and makes sure that empty strings get handled as `nil` and therefore still fall back to the default. – spickermann Aug 18 '19 at 12:53
  • it doesn't seems to be working if param[sheet_name] is null it writes sheet_name = null – Amal T S Aug 18 '19 at 14:09
  • Is the attribute really named just `name`? Or should it be `@sheet.sheet_name = ...`? And how and where do you actually set `sheet_name`? – spickermann Aug 18 '19 at 14:11
  • Yes, there is a shortcut for that: Exactly to code I wrote above in my answer. Therefore I am sure there must be another issue in your code outside of the example that you posted. – spickermann Aug 18 '19 at 14:23
0

you can add a default value for that column in the database by

  1. create a migration using rails g migration AddDefaultValueForSheetName
  2. add that line change_column_default :sheets, :name, value you want to be default
  3. run the migration using rake db:migrate
  4. in the controller use something like
  if @sheet = Sheet.where(id: sheet_id).first_or_initialize
        @sheet.user_id = user_id
        @sheet.name = params[sheet_name] if params[sheet_name].present?
        @sheet.save
  end
Hisham Magdy
  • 144
  • 4