Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7889059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:59:57+00:00 2026-06-03T05:59:57+00:00

I’m trying to make an attribute setter in an ActiveRecord model wrap its value

  • 0

I’m trying to make an attribute setter in an ActiveRecord model wrap its value in the text2ltree() postgres function before rails generates its sql query.

For example,

post.path = "1.2.3"
post.save

Should generate something like

UPDATE posts SET PATH=text2ltree('1.2.3') WHERE id = 123 # or whatever

What’s the best way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:59:59+00:00Added an answer on June 3, 2026 at 5:59 am

    EDIT: To achieve exactly what you are looking for above, you’d use this to override the default setter in your model file:

    def path=(value)
      self[:path] = connection.execute("SELECT text2ltree('#{value}');")[0][0]
    end
    

    Then the code you have above works.

    I’m interested in learning more about ActiveRecord’s internals and its impenetrable metaprogramming underpinnings, so as an exercise I tried to accomplish what you described in your comments below. Here’s an example that worked for me (this is all in post.rb):

    module DatabaseTransformation
      extend ActiveSupport::Concern
    
      module ClassMethods
        def transformed_by_database(transformed_attributes = {})
    
          transformed_attributes.each do |attr_name, transformation|
    
            define_method("#{attr_name}=") do |argument|
              transformed_value = connection.execute("SELECT #{transformation}('#{argument}');")[0][0]
              write_attribute(attr_name, transformed_value)
            end
          end
        end
      end
    end
    
    class Post < ActiveRecord::Base
      attr_accessible :name, :path, :version
      include DatabaseTransformation
      transformed_by_database :name => "length" 
    
    end
    

    Console output:

    1.9.3p194 :001 > p = Post.new(:name => "foo")
       (0.3ms)  SELECT length('foo');
     => #<Post id: nil, name: 3, path: nil, version: nil, created_at: nil, updated_at: nil> 
    

    In real life I presume you’d want to include the module in ActiveRecord::Base, in a file somewhere earlier in the load path. You’d also have to properly handle the type of the argument you are passing to the database function. Finally, I learned that connection.execute is implemented by each database adapter, so the way you access the result might be different in Postgres (this example is SQLite3, where the result set is returned as an array of hashes and the key to the first data record is 0].

    This blog post was incredibly helpful:

    http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/

    as was the Rails guide for plugin-authoring:

    http://guides.rubyonrails.org/plugins.html

    Also, for what it’s worth, I think in Postgres I’d still do this using a migration to create a query rewrite rule, but this made for a great learning experience. Hopefully it works and I can stop thinking about how to do it now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.