I have a method that converts a URL entered/pasted into a micropost form by a user. The URL they enter or paste is converted in an appropriate URL before the micropost is posted/saved. I can then use for some other things in my APP.
The thing is I only need the before_save method to run if a link has been entered / pasted. When no link is entered or pasted a few things in my app break. I would like to know what the appropriate conditional statement to use would be? and possible an example..
Here is my model..
class Micropost < ActiveRecord::Base
include OgpObjectsHelper
belongs_to :user
has_many :comments, :dependent => :destroy
has_one :photo, :dependent => :destroy
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['image'].blank? },
:allow_destroy => true
attr_accessor :username
attr_accessible :content, :user_id, :poster_id, :username, :link, :photo_attributes
validates :content, :presence => true, :length => { :maximum => 10000 }
validates :user_id, :presence => true
default_scope :order => 'microposts.created_at DESC'
auto_strip_attributes :content,
:nullify => true,
:strip => false
before_save :clean_up_video_link
protected
def clean_up_video_link
self.link = get_video_data(link)
end
end
Kind regards.
If I understand question correctly, that’s what you can use