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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:21:40+00:00 2026-06-03T22:21:40+00:00

Model I class TimeLog < ActiveRecord::Base has_one :custom_time_fields, :dependent => :destroy end Model II

  • 0

Model I

class TimeLog < ActiveRecord::Base
    has_one :custom_time_fields,  :dependent => :destroy
end

Model II

class CustomTimeFields <  ActiveRecord::Base
   belongs_to :time_log
end

above design in terms of database will be

timelogs table + custom_time_field_id(foreign key)

custom_time_fields

So when i delete timelog entry its associated ‘custom_time_field’ will be auto deleted by rails

But i want database design like following

Table I:

time_logs

Table II

custom_time_fields (having time_log_id as foreign key)

Table I will have Zero or one association of Table II

How can i represent above database design in Rails models, so that when i delete time_log, associated custom_time_field entry is auto deleted.

  • 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-03T22:21:42+00:00Added an answer on June 3, 2026 at 10:21 pm

    You have to switch the has_one and belongs_to relations of your models to change the table containing the foreign key (the model with the relation belongs_to is the one holding the foreign key). Do not forget to adapt your migrations according to the change (to declare the time_log_id column).

    I think the “zero or one” relation you’re looking for is the has_one relation. This relation is not mandatory (unless you add a validation to it).

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

Sidebar

Related Questions

MODEL: class Listing < ActiveRecord::Base serialize :details validates_presence_of :name, :details end VIEW: <% form_for(@listing)
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
This is my Action model: class Action < ActiveRecord::Base end class Fold < Action
I have a model class as shown below: class Product < ActiveRecord::Base belongs_to :user;
User model class User < ActiveRecord::Base has_many :medicalhistory end Mdedicalhistory model class Medicalhistory <
User model: class User < ActiveRecord::Base named_scope :clients, :conditions => roles_users.role_id = #{Role.find_by_name('client').id} end
I have this model: class Story < ActiveRecord::Base validates_presence_of :name , :link end Where
Tools model class Tools < ActiveRecord::Base validates_presence_of :title, :description, :image_url end When I submit
I have a model : class HotelTheme < ActiveRecord::Base has_many :hotels end I want
Table City Model class TableCity < ActiveRecord::Base has_many :near_cities, :foreign_key => nearcity_id end NearCity

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.