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.