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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:12:42+00:00 2026-06-15T10:12:42+00:00

I have set up 2 models in Rails: class Category < ActiveRecord::Base attr_accessible :name

  • 0

I have set up 2 models in Rails:

class Category < ActiveRecord::Base
  attr_accessible :name
  has_many :platforms
end

and

class Platform < ActiveRecord::Base
    attr_accessible :name, :url, :country
    validates :name, :presence => true, :length => { :minimum => 5 }
    validates :url, :presence => true, :length => { :minimum => 5 }
    belongs_to  :categories
end

This is my platform controller :

class PlatformsController < ApplicationController
  # GET /platforms
  # GET /platforms.json
  def index
    @platforms = Platform.all
    @categories = Category.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @platforms }
    end
  end

  # GET /platforms/1
  # GET /platforms/1.json
  def show
    @platform = Platform.find(params[:id])
    @categories = Platform.categories

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @platform }
    end
  end

  # GET /platforms/new
  # GET /platforms/new.json
  def new
    @platform = Platform.new
    @categories = Category.all

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @platform }
    end
  end

  # GET /platforms/1/edit
  def edit
    @platform = Platform.find(params[:id])
    @categories = Category.find(:all)
  end

  # POST /platforms
  # POST /platforms.json
  def create
    @platform = Platform.new(params[:platform])
    #@categories = Category.new(params[:name])
    @categories = @platform.categories.create(params[:categories])

    respond_to do |format|
      if @platform.save
        format.html { redirect_to @platform, notice: 'Platform was successfully created.' }
        format.json { render json: @platform, status: :created, location: @platform }
      else
        format.html { render action: "new" }
        format.json { render json: @platform.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /platforms/1
  # PUT /platforms/1.json
  def update
    @platform = Platform.find(params[:id])
    @categories = Category.find(:all)

    respond_to do |format|
      if @platform.update_attributes(params[:platform])
        format.html { redirect_to @platform, notice: 'Platform was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @platform.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /platforms/1
  # DELETE /platforms/1.json
  def destroy
    @platform = Platform.find(params[:id])
    @platform.destroy

    respond_to do |format|
      format.html { redirect_to platforms_url }
      format.json { head :no_content }
    end
  end
end

I do not understand what I do wrong, but it doesnt correctly assign categories to platforms, and also in the platforms index view, when I try to use :

<%= platform.categories  %>

it gives me error cannot find Category with id= “and here the respective id”

I am really confused since I followed tutorial for this one.

I use Rails 3.2.8

  • 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-15T10:12:43+00:00Added an answer on June 15, 2026 at 10:12 am

    Without your view, I can’t say for sure what it is you’re trying to do exactly. Most importantly, what is in your params[:categories] hash? Given the name, it sounds like you intended for it to be multiple categories. However, your code is written as if you intended it to be a single set of attributes which describe one Category.

    Since I can’t say for sure what you want to do, I’ll answer your question by explaining what you are doing. Maybe that will help you figure out how to fix it.

    Your create code currently looks like this:

    # POST /platforms
    # POST /platforms.json
    def create
      @platform = Platform.new(params[:platform])
      #@categories = Category.new(params[:name])
      @categories = @platform.categories.create(params[:categories])
    

    The first line creates the new Platform and is easy. Skipping over the comment to the third line. This is probably what’s tripping you up.

    You are selecting the associations for your newly created Platform and trying to create a new category with attributes as stored in the params[:categories] hash. I’m afraid this is not allowed. (I think it throws an ActiveRecord::RecordNotSaved exception, but I could be wrong.) You can not create on a @platform which hasn’t been persisted yet. Instead, I think you want build.

    Here is the relevant documentation:
    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    The difference between create and build is that build just sets up the association without actually saving it to the database yet. create saves it immediately. The nice thing about build is that you don’t actually have to save it yourself. It tags along for free when you call @platform.save or @platform.update_attributes. Also, save is automatically wrapped in a transaction, so it won’t create the new Category if it fails to create the new Platform for whatever reason.

    The next interesting thing is that you are assigning the result of your create to @categories. I don’t think this is what you want either. You don’t need to save the new Category because it tags along with your @platform. However, if the save of the platform fails, then you are going to re-render your new view with this value of @categories whereas in new you set @categories = Category.all. This could certainly cause some confusion on the new view after a failed create.

    In summary, I think your create code should look something like the following.

    # POST /platforms
    # POST /platforms.json
    def create
      @platform = Platform.new(params[:platform])
      @platform.categories.build(params[:categories])
    
      respond_to do |format|
        if @platform.save
          format.html { redirect_to @platform, notice: 'Platform was successfully created.' }
          format.json { render json: @platform, status: :created, location: @platform }
        else
          @categories = Category.all
          format.html { render action: "new" }
          format.json { render json: @platform.errors, status: :unprocessable_entity }
        end
      end
    end
    

    If you’re params[:categories] is not a hash of category attributes and is actually a comma delimited string of category names, then you would want to do something like the following instead of my second line above:

    params[:categories].split(",").each do |category|
      @project.categories.build(name: category)
    end
    

    You may also want to check out accepts_nested_attributes_for which can DRY out your controller even more.
    http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

    I hope that helps.

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

Sidebar

Related Questions

I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
In my Rails models I have: class Song < ActiveRecord::Base has_many :flags has_many :accounts,
I have a two models set up like this: class User < ActiveRecord::Base #
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
I have the following models: class Match < ActiveRecord::Base has_and_belongs_to_many :teams end And class
I have my models setup like so: class User < ActiveRecord::Base has_many :posts, :foreign_key
This is in Ruby 1.9.3p194, with Rails 3.2.8 app/models/owner.rb: class Owner < ActiveRecord::Base attr_accessible
I have three models presented in a multi-model form. class Parent < ActiveRecord::Base has_many
I have 3x models: class Heuristic < ActiveRecord::Base has_many :footnotes has_many :references, :through =>
I have three models: RaceCards, Races and Wagers. class RaceCard < ActiveRecord::Base has_many :races

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.