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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:18:32+00:00 2026-05-28T16:18:32+00:00

I have three models: category.rb class Category include Mongoid::Document # Relationships has_many :posts, :autosave

  • 0

I have three models:

category.rb

class Category
  include Mongoid::Document

  # Relationships
  has_many :posts, :autosave => true
  has_many :boards, :autosave => true
  accepts_nested_attributes_for :boards
  accepts_nested_attributes_for :posts


  #fields
  field :name

  #attr
  attr_accessible :name
end

My model board.rb

class Board
 include Mongoid::Document

 # Relationships
  has_many :posts, :dependent => :destroy , :autosave => true
  accepts_nested_attributes_for :posts
  belongs_to :category

  #fields
  field :name
  field :description

  #attr
  attr_accessible :name, :posts_attributes, :description, :category_id

end

My post.rb

class Post
 include Mongoid::Document

 # Relationships
 belongs_to :board
 belongs_to :category
 belongs_to :user

 #fields  
 field :content

 #attr
 attr_accessible :content :board_id, :category_id

end

In my posts_controller

def create

   @post = current_user.posts.new(params[:post])
   @board = @post.board
   @board.user = current_user

 if @board.category_id? 
  @post.category_id = @board.category_id
 end

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

end

In my view new action:

<%= form_for(@post) do |f| %>
 <%= f.text_area :content %>
 <%= f.collection_select :board_id, Board.where(:user_id => current_user.id), :id, :name%>
 <%= f.submit :id => "button_submit_pin_edit" %>
<% end %>

The boards in select field, may or may not have a parent category already assigned.

I want get the attributes from category (the name of the category for this event) in my post view without using a select field, or an input field.

with this code in posts.controller.rb

if @board.category_id? 
  @post.category_id = @board.category_id
 end

I see in my console for Post.first e.j.:

<Post _id: 4f1d96241d41c8280800007c, _type: nil, created_at: 2012-01-23 17:17:24 UTC, user_id: BSON::ObjectId('4f0b19691d41c80d08002b20'), board_id: BSON::ObjectId('4f1455fa1d41c83988000510'), category_id: BSON::ObjectId('4f1c2d811d41c8548e000008'), content: "nuevo post"> 

If I write:

post = Post.first

and

post.board

I get the object board fine. This does works fine.

but If I write:

post.category

I get:

=> nil

I have try in view new post add hidden field:

hidden_field(:category, :name)

How can I get the params of object category?
Thanks

  • 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-28T16:18:34+00:00Added an answer on May 28, 2026 at 4:18 pm

    Don’t set it using the ID fields. Instead of this in your PostsController:

    if @board.category_id? 
      @post.category_id = @board.category_id
    end
    

    Try this line:

    @post.category = @board.category
    

    Which will set the @post.category belongs_to relationship to point to @board.category assuming it exists (is not nil). Mongoid will handle the setting of the category_id field for you. Read the mongoid documentation about relationships for a more detailed explanation.

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

Sidebar

Related Questions

I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have this model: class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent =
I have the following three models (Rails 2.3.8) class Outbreak < ActiveRecord::Base has_many :incidents,
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have 2 models: class Video < ActiveRecord::Base belongs_to :categories, :foreign_key => category, :class_name
I have the following models: class Category(models.Model): name = models.CharField(max_length=40) class Item(models.Model): name =

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.