开发者

Ruby on Rails - concatenate strings on save to database upon user clicking create action

开发者 https://www.devze.com 2023-03-22 02:29 出处:网络
Its late and I probably should sleep on this. Easy one I have 3 fields in a form which a user fills in. Once they click the create button these records are saved to the database. Simple.

Its late and I probably should sleep on this. Easy one

I have 3 fields in a form which a user fills in. Once they click the create button these records are saved to the database. Simple.

However I want the data from these three fields at the same time to be concatenated together, nothing fancy..and inserted to the database at the same time as the other recor开发者_开发百科ds. This should reflect back to the user on the show page after they create.

So I need an action to concatenate 3 db columns lets say column names are firstname, surname and DOB. table name PeopleDetails

I have tried building the model using after_create, before_save, built into the model, but nada. suggestions. I think I will come back and revisit this after some sleep


Assuming you have a column (model attribute) called full_name than you can combine all together on a model create/save via:

class User < ActiveRecord::Base
  before_save :concatenate_details

  def concatenate_details
     self.full_name = "#{firstname} #{surname} #{dob}"
  end

end
0

精彩评论

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