Section 229. If, upon the trial of an action against a city or town, the plaintiff recovers damages for an injury to his person or property caused by reason of a defect in a highway, within the location of a railroad, and if the corporation owning the railroad is liable for such damages, and has had reasonable notice to defend the action, the city or town may recover from the corporation such damages and the costs of both plaintiff and defendant in the action.