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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:49:30+00:00 2026-05-26T12:49:30+00:00

In my Rails 3.1.1 project I have an ActiveModel that talks to API (ripped

  • 0

In my Rails 3.1.1 project I have an ActiveModel that talks to API (ripped from Paul Dix’s book, shortened for readability):

class Job
  include ActiveModel::Validations
  include ActiveModel::Serializers::JSON

  ATTRIBUTES = [ :id,
                 :title,
                 :description,
                 :company_id ]

  attr_accessor *ATTRIBUTES

  validates_presence_of     :title, :description
  validates_numericality_of :company_id, :id

  def initialize(attributes = {})
    self.attributes = attributes
  end

  def attributes
    ATTRIBUTES.inject(
      ActiveSupport::HashWithIndifferentAccess.new
      ) do |result, key|

      result[key] = read_attribute_for_validation(key)
      result
      end
  end

  def attributes=(attrs)
     attrs.each_pair {|k, v| send("#{k}=", v)}
  end

  def read_attribute_for_validation(key)
    send(key)
  end

  # More method definitions...
end

I instantiate @job in my controller, new action (company_id is a segnment key in the route: /companies/:company_id/jobs/new) like this:

@job = Job.new(company_id: params[:company_id])

Then, using CanCan, I check user’s permissions to create to create a job. Basically, CanCan checks if current_user’s company_id attribute matches job’s company_id. This check fails because @job.company_id is returned as String.

Certainly, I can use params[:company_id].to_i while instantiating the object, but this seems like a workaround that I would have to repeat later.

Question: is there a way to make my Job ActiveModel more “type-aware” and make it return int for @job.company_id call?

I googled around, checked activemodel source code, but doesn’t seem to find an answer. Any help is greatly appreciated.

Update
I was thinking more of something like schema block for ActiveModel, just like the one in ActiveResource.

  • 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-05-26T12:49:31+00:00Added an answer on May 26, 2026 at 12:49 pm
    attr_accessor *ATTRIBUTES
    

    create a method like this:

    def company_id
      @company_id
    end
    

    You can just override that with

    def company_id
      @company_id.to_i
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class defined in a file my Rails project's lib dir that
In my rails 3.1 project I have a Book model that has a ID,
In my rails project I have a presenter method that has a helper view
I have a rails project that I would like to set up email notifications
I have a Rails project that is basically a simple web app for a
I have a rails 3.1 project that I'd like to run some jasmine specs
I have a Ruby on Rails project that has been untouched for a while,
In my Rails 3 project I have the following code for My controller: class
I have a rails project that uses mongodb, the issue i am having is
I have a javascript plugin in my rails project that loads an image this

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.