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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:17:25+00:00 2026-05-28T07:17:25+00:00

I have 2 models The first model category.rb class Category include Mongoid::Document # Relationships

  • 0

I have 2 models

The first model category.rb

class Category
  include Mongoid::Document

 # Relationships
  has_many :boards, :dependent => :destroy , :autosave => true
  accepts_nested_attributes_for :boards

  #fields
  field :name

  #attr
  attr_accessible :name, :boards_attributes
end

The second model its board.rb

class Board
 include Mongoid::Document

#Relationships
 belongs_to :category

  #fields
  field :name
  field :description

  #attr
  attr_accessible :name, :description

end

I have in edit board view the next form:

<%= form_for [@board], :url => user_board_path do |f| %>
 <%= f.text_field :name %>
 <%= f.text_area :description, :cols =>72, :rows => 5, %>
 <%= f.collection_select :category_id, Category.all, :id, :name%>
<% end %>

and I have in update action from boards_controller.rb the next:

def update
  @board = Board.find(params[:id])
  @category = Category.find(params[:category_id])
  @board.category_id = @category

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

Why I get the @board.category_id nil? I want update the @board.category_id with the value that I choose in select

  • 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-28T07:17:25+00:00Added an answer on May 28, 2026 at 7:17 am

    The problem is @board.category_id = @category (you set an Object to an id field). It should be

    @board = Board.find(params[:id])
    @category = Category.find(params[:category_id])
    @board.category = @category
    

    or if @category is not used in the controller nor in the view, you could write

    @board = Board.find(params[:id])
    @board.category_id = params[:category_id]
    

    The second solution removes a “SELECT” request on the Categories

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

Sidebar

Related Questions

I have three models: category.rb class Category include Mongoid::Document # Relationships has_many :posts, :autosave
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have a simple category model: class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField()
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have 2 models which have a has_and_belongs_to_many relation: class Category < ActiveRecord::Base has_and_belongs_to_many
I have a model class ServiceRequest < ActiveRecord::Base has_many :services accepts_nested_attributes_for :services ... end
If I have the following Model: class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)

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.