0

Inside a loop in my views I have the following code, that combines a few variables for each record:

<% $theurl = newbrandurl + "/?utm_campaign=" + unique_id + "&utm_source=sales" %>

<%= link_to $theurl, class: "btn btn-success" do %>
 Order
<% end %>

But that's only in my views. What I want to do is to get this done outside of my views after an Order get's saved to the DB and save the link to the DB. Is that possible?

Edit:

My Model:

class Sale < ActiveRecord::Base
attr_accessible :url
after_create :save_link

 def save_link
  brandurl = Brand.where(:company => @sale.brand).pluck(:url)
  newbrandurl = brandurl.shift.strip
  unique_id = [@sale.token, @sale.created_at.strftime('%d%m%y-%H:%M:%SUTC')].join("&")
  self.save_link = newbrandurl + "/?utm_campaign=" + unique_id + "&utm_source=sales"
 end
end
CottonEyeJoe
  • 720
  • 1
  • 7
  • 28

1 Answers1

0

You need to add a :after_create callback

Class ModelName < ActiveXXX
    attr_accessible :url # the attribute name for storing the link
    after_create :save_link

    def save_link
        self.save_link = newbrandurl + "/?utm_campaign=" + unique_id + "&utm_source=sales"
    end
end

you will however, need to add the object data(newbrandurl, unique_id), however relevant if its coming from some other source

Sudipta Mondal
  • 2,550
  • 1
  • 19
  • 20