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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:59:52+00:00 2026-06-06T21:59:52+00:00

I have an model with a relation, and I want to instantiate a new

  • 0

I have an model with a relation, and I want to instantiate a new object of the relations type.

Example: A person has a company, and I have a person-object: now I
want to create a company-object.

The class of the companyobject is defined in the relation, so I don’t think I should need to ‘know’ that class, but I should be able to ask the person-object to provide me with a new instance of type company? But I don’t know how.

This is -I think- the same question as New model object through an association , but I’m using PHPActiveRecord, and not the ruby one.


Reason behind this: I have an abstract superclass person, and two children have their own relation with a type of company object. I need to be able to instantiate the correct class in the abstract person.

A workaround is to get it directly from the static $has_one array:

$class   = $this::$has_one[1]['class_name'];
$company = new $class;

the hardcoded number can of course be eliminated by searching for the association-name in the array, but that’s still quite ugly.


If there is anyone who knows how this is implemented in Ruby, and how the phpactiverecord implementation differs, I might get some Ideas from there?


Some testing has revealed that although the “search my classname in an array” looks kinda weird, it does not have any impact on performance, and in use it is functional enough.

  • 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-06T21:59:53+00:00Added an answer on June 6, 2026 at 9:59 pm

    You can also use build_association() in the relationship classes.
    Simplest way to use it is through the Model’s __call, i.e. if your relation is something like $person->company, then you could instantiate the company with
    $company = $person->build_company()

    Note that this will NOT also make the “connection” between your objects ($person->company will not be set).
    Alternatively, instead of build_company(), you can use create_company(), which will save a new record and link it to $person

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

Sidebar

Related Questions

I have a user model which has association with groups. The relation is given
I have three table in SQL database such as table Person has relation one
I have an Account Model which has some properties and relations, and i have
I have two model News and Category. And they have habtm relation. I want
In my ruby model I have a has_and_belongs_to_many relation with a model search: has_and_belongs_to_many
I have the following relation in my model: A user can have several consumers
I have a simple SQL relational model with a many to many relation. Here
I have a Profile model and two ActiveRecord::Relations of this model. SQL request of
I have model with a location, which itself has a latitude and longitude. What
I have model Foo which has field bar. The bar field should be unique,

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.