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 8384845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:30:55+00:00 2026-06-09T17:30:55+00:00

I have a model Download , with a table downloads . downloads has a

  • 0

I have a model Download, with a table downloads. downloads has a field called ip_address, which stores an ip address as an integer. I want to set up an IpAddress model, but without a ip_addresses table, so I can do stuff like

Download.find(1).ip_address.to_s # '127.0.0.1'
Download.find(1).ip_address.to_i # 2130706433
Download.find(1).ip_address.downloads # SELECT * FROM downloads WHERE ip_address='2130706433'
IpAddress.find(2130706433).downloads # SELECT * FROM downloads WHERE ip_address='2130706433'

I want it to behave like:

class Download < ActiveRecord::Base
  belongs_to :ip_address, :foreign_key => :ip_address
end

class IpAddress < ActiveRecord::Base
  set_primary_key :ip_address
  has_many :downloads, :foreign_key => :ip_address
end

but without having a useless table of ip addresses.

Is this possible?

EDIT

I found that ruby already has a IPAddr class.

So I did this:

require 'ipaddr'

class Download < ActiveRecord::Base
  attr_accessible :ip, ...

  def ip
    @ip ||= IPAddr.new(read_attribute(:ip), Socket::AF_INET)
  end

  def ip=(addr)
    @ip = IPAddr.new(addr, Socket::AF_INET)
    write_attribute(:ip, @ip.to_i)
  end

  def self.for_ip(addr)
    where(:ip => IPAddr.new(addr, Socket::AF_INET).to_i)
  end

end

Then I can do lots of cool stuff

Download.new(:ip => '127.0.0.1').save
Download.for_ip('127.0.0.1').first.ip.to_i # 2130706433
  • 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-09T17:30:57+00:00Added an answer on June 9, 2026 at 5:30 pm
    IpAddress.find(2130706433).downloads # SELECT * FROM downloads WHERE ip_address='2130706433'
    

    This is totally a semantics issue, but this should probably change if you have no IpAddress table (i.e. how can we find the IpAddress object 2130706433 in the database if there is no IpAddress table – unless you make IpAddress a container rather than a specific single ipaddress, otherwise do something like instantiate new ones with a constructer like IpAddress(2130706433).downloads).

    Otherwise, though, I don’t see any problems in not having the IpAddress table. Why do you need it to be belongs_to, rather than just another column?

    You can keep the models/objects if you wish to access them in similar ways:

    class Download < ActiveRecord::Base
      ##Whatever Download-model-specific code you have...
      def ip_address
        #If nil, initialize new object. Return object.
        @ip_address ||= IpAddress(ip_address_val)
      end
    end
    
    class IpAddress
      def initialize(address)
        @value = address
      end
      def downloads
        Download.where(:ip_address_val => self.value)
      end
    end
    

    EDIT:
    You can override the accessor, like you’re asking. You just have to be careful in your code to be particular about what you’re asking for.
    See this doc: http://ar.rubyonrails.org/classes/ActiveRecord/Base.html
    Under section “Overwriting default accessors”

    Basically, if you do override the value, and if you wish to access the DB value, you use read_attribute(attr_name), so the code might look like this:

    class Download < ActiveRecord::Base
      ##Whatever Download-model-specific code you have...
      def ip_address
        #If nil, initialize new object. Return object.
        @ip_address ||= IpAddress(read_attribute(:ip_address))
      end
    end
    
    class IpAddress
      def initialize(address)
        @value = address
      end
      def downloads
        Download.where(:ip_address => self.value)
      end
    end
    

    Though things might get a little confusing in your code if you aren’t careful.

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

Sidebar

Related Questions

I have model Foo which has field bar. The bar field should be unique,
I have model with a location, which itself has a latitude and longitude. What
I have model Article it has field title with some text that may contain
I have a model which can download data from a server and thus an
I have model: # encoding: utf-8 class Tag include Mongoid::Document field :name, type: String
Lets say I have model inheritance set up in the way defined below. class
If I have Model.objects.all() I want to get only one object for any content_object=foo,
In an ASP.NET MVC I have a database table. I want to have a
i have a problem,,i try to make a table using datatable...i've been download from
I have a model which allows users to upload an .ics file to a

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.