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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:27:41+00:00 2026-05-24T12:27:41+00:00

I have a join model that connects Authors to Products. It’s called contracts. I

  • 0

I have a join model that connects Authors to Products. It’s called contracts. I want to create the contract as soon as the product is created, so in my product model I have:

     after_save :create_contract

     def create_contract
        contract = Contract.new(
          :product_id => self.id,
          :author_id => @author_id
        )
        contract.save   
     end

It seemed simple enough to me, but :author_id always comes up nil when it’s ready to go into the database. I’ve tried a couple different ways of setting it, nothing seems to work. I’m guessing it’s related to how I’m submitting it with the products form, which looks like this:

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :handle %><br />
    <%= f.text_field :handle %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :keywords %><br />
    <%= f.text_field :keywords %>
  </div>

    <div>
        <%= collection_select( "contract", "author_id", @authors, "id", "full_name") %>
    </div>

And in the controller:

  def create
    @author_id = params[:contract][:author_id]
    @product = Product.new(params[:product])
    ...
  end

Here’s what I’m seeing in the log.

Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "product"=>{"title"=>"", "handle"=>"", "description"=>"", "keywords"=>""}, "contract"=>{"author_id"=>"1"}, "commit"=>"Create Product"}
  SQL (1.1ms)  INSERT INTO "products" ("created_at", "description", "handle", "keywords", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Mon, 08 Aug 2011 04:37:09 UTC +00:00], ["description", ""], ["handle", ""], ["keywords", ""], ["title", ""], ["updated_at", Mon, 08 Aug 2011 04:37:09 UTC +00:00]]
  SQL (0.7ms)  INSERT INTO "contracts" ("author_id", "created_at", "product_id", "updated_at") VALUES (?, ?, ?, ?)  [["author_id", nil], ["created_at", Mon, 08 Aug 2011 04:37:09 UTC +00:00], ["product_id", 5], ["updated_at", Mon, 08 Aug 2011 04:37:09 UTC +00:00]]

Any ideas on where the problem is?

class Product < ActiveRecord::Base
  has_many :authors, :through => :contracts

and

class Author < ActiveRecord::Base
  has_many :products, :through => :contracts

and

class Contract < ActiveRecord::Base
  belongs_to :author
  belongs_to :product
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-24T12:27:42+00:00Added an answer on May 24, 2026 at 12:27 pm

    Your @author_id in create_contract is the Product model, and therefore not in the same scope as your controller.

    Try putting the following in your models:

    class Product < ActiveRecord::Base
      has_one :contract, :dependent => :destroy
      has_one :author, :through => :contract
      accepts_nested_attributes_for :contract
    end
    
    class Contract < ActiveRecord::Base
      belongs_to :product
      belongs_to :author
    end
    
    class Author < ActiveRecord::Base
      has_many :contracts, :dependent => :destroy
      has_many :products, :through => :contracts
    end
    

    Then in your form:

    ...
    <%= f.fields_for :contract do |c| %>
      <%= c.collection_select :author_id, Author.all, :id, :name %>
    <% end %>
    ...
    

    Try this and see how it goes.

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

Sidebar

Related Questions

I have users and projects resources and join table that connects them called project_members.
I have a Join Model I just Migrated called EventUsers that has an user_id,
I have an order model that belongs_to :user , a product model, a join
I have a model that acts as a join table called CELEBRATIONS. CELBERATION has_and_belongs_to_many
I have two tables that I want to join together. Table1 Year, ID, Theme,
I have two models: User MentoringRelationship MentoringRelationship is a join model that has a
I have a users model that can have many holidays through a rich join
I have a model that contains a FileField. I want to search for a
I have projects and users joined via has_many :through join model called ownerships. Each
I have 2 models: User and Book and a join model ownership which connect

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.