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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:17:18+00:00 2026-06-10T06:17:18+00:00

I’m trying to create a simple relationship but with a model in a subdirectory

  • 0

I’m trying to create a simple relationship but with a model in a subdirectory under Rails 3.2

I have a User class and each user has an email subscription.. Because There will be other types of subscriptions (which also fill be added to users) I put them in a subdirectory . (My User happens to come from Devise but I suspect that won’t matter.)

Here’s the code

In model/user.rb

class User < ActiveRecord::Base
  has_one :email_sub

  before_create :build_email_subscription

  private

  def build_email_subscription
    Subscription::EmailSub.create(:is_subscribed => true, :user_id => self.id)
    true
  end
end

Note that I have also created a way to add a default subscription.

In model/subscriptions/email_sub.rb

class Subscriptions::EmailSub < ActiveRecord::Base
  belongs_to :user
end

In addition to the migrations to create the two classes I created the following migration for the relationship.

class AddSubscriptionToUser < ActiveRecord::Migration
  def change_table :subscriptions_email_sub do |t|
    t.referneces :users
  end
end

However, this doesn’t seem to be working. It doesn’t seem to be generating the user_id column.

  • 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-10T06:17:19+00:00Added an answer on June 10, 2026 at 6:17 am

    Looks like I got it to work. I’m sharing and annotating my result for others to learn.

    In model/user.rb

    class User < ActiveRecord::Base
      has_one :email_sub, :class_name => "Subscriptions::EmailSub"
    
      before_create :build_email_subscription
    
      private
    
      def build_email_subscription
        build_email_sub(:announcements => true, :notifications => true, :master => true)
        true
      end
    end
    

    I expressly include the class_name to find the model in the subdirectory. I used the build_<attribute> and passed in the parameters for three subscriptions (later to come in from a form but for now just setting defaults).

    In model/subscriptions/email_sub.rb

    class Subscriptions::EmailSub < ActiveRecord::Base
      attr_accessible :announcements, :daily_deals, :master
      belongs_to :user
    end
    

    (I left the attributes out above since they’re not really relevant to the issue but I’ll expressly include them here for beginning users.)

    Instead of a new migration I modified the original migration for the EmailSubs model.

    class CreateSubscriptionsEmailSubs < ActiveRecord::Migration
      def change
        create_table :subscriptions_email_subs do |t|
          t.boolean :master
          t.boolean :daily_deals
          t.boolean :announcements
    
          t.references :user    # I added this line to the generated migration
          t.timestamps
        end
      end
    end
    

    I added the t.references line. Note that I had it plural before and not singular (in a migration the table name needs to be plural, the field singular). t.references will know to look for the _id field given .

    Hopefully this can help save some others some time.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to create an if statement in PHP that prevents a single post

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.