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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:49:43+00:00 2026-06-04T07:49:43+00:00

My migration right now : class CreateActivities < ActiveRecord::Migration def self.up create_table :activities do

  • 0

My migration right now :

class CreateActivities < ActiveRecord::Migration
  def self.up
    create_table :activities do |t|
      t.integer :account_id, :null => false
      t.integer :target_id, :null => false
      t.string  :target_type, :null => false
      t.string  :event_type, :null => false
      t.integer :employee_id
      t.string  :name
      t.timestamps
    end

    add_index :activities, [:target_id, :target_type]

With this I can now call a target like so :

Activity.first.target

And it will bring up the target_id, based on the target_type.

How would I do the opposite of that so that I can select a target, and if it has any associated Activities, they will show up?

Like so :

Job.find(1234).activities
# Where Job.find(1234) is the target_id of many activities.
  • 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-04T07:49:45+00:00Added an answer on June 4, 2026 at 7:49 am

    If I understand right, this should do it:

    class Job < ActiveRecord::Base
      has_many :activities, :conditions => ['target_type = ?', 'Job'], :as => :target
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need suggestion for migration few modules of asp.net mvc 3 application: Right now we
I am a PHP developer migrating to C# ASP.Net Framework. As of right now,
Right now, I'm in the middle of actually building a Project Management application, and
So I just started with Rails and right now looking over HABTM. I am
I'm building a web application right now for my company and for whatever reason,
I'm using rails 3, and began my application with ActiveRecord. Now, I have many
Right now my products model has a string column of category, with the form
I am working on an app right now which has the potential to grow
I have a job right now that will never quit. It involves running a
I'm working on deploying my first Rails application right now, and somewhere along the

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.