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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:47:21+00:00 2026-06-13T22:47:21+00:00

I am looking to setup a relationship where a user can create a post

  • 0

I am looking to setup a relationship where a user can create a post and tag that post. That post and those tags belong to that user.

I’ve been trying to debug this for the last three days. I don’t know why when I save my Post, the user_id does not populate in the Tag model. I added the user_id into the Tag model.

I apologize in advance for the excessive code.

User – has_many posts; has_many tags

Post – belongs_to user; has_many taggings; has_many tags through taggings

Tag – has many taggings; has_many posts through taggings; belongs_to user

Tagging – belongs_to post; belongs_to tag

ActiveRecord::Schema.define(:version => 20121031012555) do

  create_table "posts", :force => true do |t|
    t.integer  "user_id"
    t.string   "summary"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  create_table "taggings", :force => true do |t|
    t.integer  "post_id"
    t.integer  "tag_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "taggings", ["post_id"], :name => "index_taggings_on_post_id"
  add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"

  create_table "tags", :force => true do |t|
    t.string   "name"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "username"
    t.string   "email"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.datetime "created_at",        :null => false
    t.datetime "updated_at",        :null => false
    t.string   "role"
  end

end

In the post Model:

def tag_ids=(tags_string)
    self.taggings.destroy_all

    tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
    tag_names.each do |tag_name|
      tag = Tag.find_or_create_by_name(tag_name)
      tagging = self.taggings.new
      tagging.tag_id = tag.id
    end
end
  • 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-13T22:47:22+00:00Added an answer on June 13, 2026 at 10:47 pm

    I apologize if I’m missing something, but it looks like you’re not passing user id to the tag when it gets created. With rails 3 you can do your find_or_create_by for more than one attribute, like so:

    tag = Tag.find_or_create_by_name_and_user_id(tag_name, user_id)
    

    In addition, if you only want to build the Tag object, but not immediately save it to the DB, you may use the “find_or_initialize_by…” method, like so:

    tag = Tag.find_or_initialize_by_name_and_user(tag_name, user)
    

    Just make sure your Tag model has attr_accessible :user_id if it doesn’t already.

    Update: Sorry I just re-read your question, and you stated that you do pass the user_id in to the tag model. However I don’t see that in the code you provided; where does that occur?

    Update #2
    Based on the comment thread, your parameter map does not contain the user_id; this is, of course, because the parameter map is built from the form inputs, which apparently does not contain an input for the user.

    I’m going under the assumption that when a post is created, it should be associated with the currently logged in user. I will also assume that you have, somewhere, available a reference to the currently logged in user, which I’m going to call @current_user.

    Since it looks like you want to create the post and associated tags in one atomic operation, one thing you can do add the current user to a copy of the parameter map, like so:

    post_params = params.clone.store(:user=>@current_user)
    @post = Post.new(post_params)
    

    There may be a better way of doing this, but this should work. Note that you probably don’t need to make a clone of the parameter map; I admin I’m not sure what is best practice in this case.

    Regardless, hope that helps.

    update 3
    Since a Ruby hash is iterated over in the order in which the keys are inserted, svn’s tag_ids were being set BEFORE his user_id in to the Post model. In order to solve the problem, svn reversed the order of my previous suggestion, and ensured that the :user key was inserted before any other value. This is one possible solution (modified from svn’s gist(https://gist.github.com/4027807)):

    post_params = {:user => current_user}.merge!(params[:post].clone)
    @post = Post.new(post_params)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to setup a Server that accepts a URL with a few
I was looking at this post: https://stackoverflow.com/a/2262200 and I have a very similar setup
I am trying to create an intermediate model between two models that I'd like
I am looking to setup architecture for entity framework that will break apart the
I'm trying to create a master detail view of a linq to sql relationship
I am looking to setup Amazon EC/2 nodes on rails with Riak. I am
I'm looking to setup a custom route which supplies implicit parameter names to a
I'm looking to setup emacs to allow me to use perforce without having to
I'm looking for a good setup for learning C++ and eventually Python on Mac
I am looking to have a SharePoint Extranet Portal setup for my partner clients.

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.