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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:59:44+00:00 2026-06-03T15:59:44+00:00

Every post has only one category, and i need to access category’s name by

  • 0

Every post has only one category, and i need to access category’s name by something like

p = Post.new
p.category.name = "tech"
p.save

How to do that?


   class Category < ActiveRecord::Base
       has_many :posts, :dependent => :destroy

       attr_accessible :name, :image

    end

Post.rb

class Post < ActiveRecord::Base
    belongs_to :category

    attr_accessible :category_id, :name, :text, :lang, :image

end

Schema.rb

create_table "categories", :force => true do |t|
    t.string "name"
    t.string "image"
  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-06-03T15:59:45+00:00Added an answer on June 3, 2026 at 3:59 pm

    Your example contains a problem.

    p = Post.new
    p.category.name = "tech"
    p.save
    

    First, you create a new post. Second, you want to assign a name to the category of the post, BUT there is no category assigned. This results in a call like post.nil.name where nil would be the category object, if assigned, which isn’t the case. Since nil has no method name, you get the described error undefined method name for nil class.

    To solve this, you first need to assign a category to work on.

    p.category = Category.first or p.category_id = 1. After that, p.category will return the category object, therefore p.category.name is valid then because it is called on a category object and not on nil.

    tl;dr:

    p = Post.new
    p.category # => nil
    p.category.name # throws an error
    
    p.category = Category.first
    p.category # => <#Category ...>
    p.category.name # => 'actual name of category'
    p.category.name = 'foo' # works now
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, every comment has the same date as the original post. Theme functions PHP:
I have a custom posttype in Wordpress 3. I would like to every post
I've got a form and I need to post data to the server every
I would like to cause a post back that occurs only once by inserting
The post I am referring to is this. Not only does the first one
I'm building a basic forum where every post contains some text, first and last
I have this PHP code in a loop for every post on the admin
I have read nearly every single post about the activity not found to handle
Every time I try to post to twitter in my rails app, the first
In a typical User - Post - Comment model in Rails, every user can

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.