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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:21:03+00:00 2026-05-23T18:21:03+00:00

When user’s create a post I’d like to set the user_id attribute first. I’m

  • 0

When user’s create a post I’d like to set the user_id attribute first. I’m trying to do this using alias_method_chain on the arrtibutes method. But I’m not sure if this is right as the problem I thought this would fix is still occurring. Is this correct?

Edit:

When my users create a post they assign ‘artist(s)’ to belong to each post, using a virtual attribute called ‘artist_tokens’. I store the relationships in an artist model and a joined table of artist_ids and post_ids called artisanships.

I’d like to to also store the user_id of whomever created the artist that belongs to their post (and I want it inside the artist model itself), so I have a user_id column on the artist model.

The problem is when I create the artist for each post and try to insert the user_id of the post creator, the user_id keeps showing as NULL. Which is highly likely because the post’s user_id attribute hasn’t been set yet.

I figured to get around this I needed to set the user_id attribute of the post first, then let the rest of the attributes be set as they normally are. This is where I found alias_method_chain.

post.rb

 attr_reader :artist_tokens

  def artist_tokens=(ids)
    ids.gsub!(/CREATE_(.+?)_END/) do
      Artist.create!(:name => $1, :user_id => self.user_id).id
    end
    self.artist_ids = ids.split(",")
  end


    def attributes_with_user_id_first=(attributes = {})
      if attributes.include?(:user_id)
        self.user_id = attributes.delete(:user_id)
      end
      self.attributes_without_user_id_first = attributes
    end
    alias_method_chain :attributes=, :user_id_first

EDIT:

class ArtistsController < ApplicationController

  def index
    @artists = Artist.where("name like ?", "%#{params[:q]}%")
    results = @artists.map(&:attributes)
    results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"}

    respond_to do |format|
      format.html
      format.json { render :json => results }
    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-05-23T18:21:04+00:00Added an answer on May 23, 2026 at 6:21 pm

    In your controller, why not just do this:

    def create
      @post = Post.new :user_id => params[:post][:user_id]
      @post.update_attributes params[:post]
      ...
    end
    

    But it seems to me that it would be much better to create the artist records after you’ve done validation on the post rather than when you first assign the attribute.

    EDIT

    I would change this to a callback like this:

    class Post < ActiveRecord::Base
      attr_accessor :author_tokens
      def artist_tokens=(tokens)
        @artist_tokens = tokens.split(',')
      end
    
      after_save :create_artists
      def create_artists
        @artist_tokens.each do |token|
          ...
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

User model looks sth like this: before_save :ensure_authentication_token devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable,
User kokos answered the wonderful Hidden Features of C# question by mentioning the using
User Region Points Last_Updated 1 5 0 1-Nov-09 I want this row 2 1
User inserts new records in the table in his browser with no postback (using
User Control <%@ Control Language=C# Inherits=System.Web.Mvc.ViewUserControl %> <% using (MvcApplication1.Models.EgovtDataContext _model = new MvcApplication1.Models.EgovtDataContext())
User inputs a comma separated string and i d like to make a an
user selects an image (using fileOpenDialog) then I need to run 3 algorithms sequentially
User table contains the following attributes (dateOfBirth, race, gender, ...). We would like to
User enters pairs of values. I need to replicate the last set and renumber
User Login functionality is very common to many applications. I would like to see

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.