すべてのポストが一つだけのカテゴリを持っており、私が好きなもので、カテゴリの名前にアクセスする必要がRailsのDB関係:Post.category.name =? (未定義のメソッド `名前 ')
p = Post.new
p.category.name = "tech"
p.save
それをどのように行うには?
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
'Category'のdbフィールド' name'があるとしますか? – pduersteler
@pdsuersteler:yes – sparkle
ahh ..愚かな私... – pduersteler