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

  • Home
  • SEARCH
  • 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 5937233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:30:37+00:00 2026-05-22T15:30:37+00:00

I have a scenario where the models look like create_table :users do |t| t.string

  • 0

I have a scenario where the models look like

create_table :users do |t|
  t.string :name
  t.timestamps
end

create_table :blogs do |t|
  t.string :url
  t.string :title
  t.text :description
  t.timestamps
end

create_table :posts do |t|
  t.integer :user_id, :null => false
  t.integer :blog_id, :null => false
  t.text :post_text
end

class Blog < ActiveRecord::Base
  has_many :users, :through =>:posts
  has_many :posts, :dependent=>true
end

class User < ActiveRecord::Base
  has_many :blogs
  has_many :posts, :through=>:blogs
end

class Post < ActiveRecord::Base
  belongs_to :blog
  belongs_to :user
end

The question I have is:
1. When a user is created, I would like to create a blog for him automatically.

@user = User.find_or_create_by_name(user_name)

How do I go about creating a blog?
@blog = Blog.find_or_create_by_user_id(@user)

I am getting the following error:

undefined method `find_or_create_by_user_id' for #<Class:0x1044735b0>

@blogs = @user.blogs

gives me:

 Mysql::Error: Unknown column 'blogs.user_id' in 'where clause': SELECT * FROM `blogs` WHERE (`blogs`.user_id=1234)

I know Blogs table does not have user_id column.
But isn’t the join supposed to take care of it?
What am I doing wrong here?

Thanks for your help

  • 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-05-22T15:30:38+00:00Added an answer on May 22, 2026 at 3:30 pm

    To use the Post model as the association table, the User model needs to be tweaked to properly demonstrate the association. Once done, you could use after_create to create a new blog for a newly created user.

    class User < ActiveRecord::Base
      has_many :posts
      has_many :blogs, :through=>:posts
      after_create :add_blog
    
      private
      def add_blog
        blogs << Blog.new
      end
    
    end
    

    EDIT:

    The best I know how to handle it is to explain what I “think” the relationships are attempting to accomplish then you tell me where I’m off and we go from there.

    1) A User can “own” many blogs

    2) A blog can have many posts

    3) A post belongs to a single user and to a single blog

    4) a blog can only have one “owner” (user)

    5) Blogs can be “owned” by many users thereby giving them permission to post.

    If 1-4 are true, and 5 false… that isn’t a “has_many :through” scenario or many-to-many relationship, just one-to-many relationships.

    Accordingly, posts should not be used as an association table. There isn’t an association table needed.

    add t.integer :user_id, :null => false to the blogs table

    class Blog < ActiveRecord::Base
      belongs_to :users, 
      has_many :posts, :dependent=>:destroy # rec'd error in RoR3... replaced true with :destroy
    end
    
    class User < ActiveRecord::Base
      has_many :blogs, :dependent=>:destroy
      has_many :posts
      after_create :add_blog
    
      private
      def add_blog
        blogs << Blog.new
      end
    end
    
    class Post < ActiveRecord::Base
      belongs_to :blog
      belongs_to :user
    end
    

    If 5 is true, this would be a true many-to-many… but I don’t think that’s what you’re attempting to do.

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

Sidebar

Related Questions

I have a scenario where users of my ASP.NET web application submit testimonials consisting
So heres the scenario: Currently we have a development site with 3 models. We
Here is my scenario: I have three models Subscriber, Subscription, Plan , with has_many
Scenario: I have a habtm relationship and would like to determine if children in
I have a scenario where I have to check whether user has already opened
I have a scenario. (Windows Forms, C#, .NET) There is a main form which
I have a scenario where I need to upload a file from one web
We have a scenario where we want to display a list of items and
I have a scenario when I start 3..10 threads with ThreadPool. Each thread does
I have a scenario in a system which I've tried to simplify as best

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.