它是如此簡單的事情,但我無法找到問題所在。我有一個模型用戶(設計)誰有很多功能。看起來是這樣的:Rails n:1關係不起作用
/app/model/user.rb
class User < ActiveRecord::Base
has_many :features
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :last_name, :email, :password, :date_of_birth, :gender, :password_confirmation
# attr_accessible :title, :body
validates_inclusion_of :gender, :in => ['female', 'male'], message: 'not selected'
validates :first_name, presence: true
validates :last_name, presence: true
validates_date :date_of_birth, :on_or_before => lambda { Date.current }
end
/app/model/feature.rb
class Feature < ActiveRecord::Base
belongs_to :user
attr_accessible :content, :hits, :icon, :images, :lable, :rank, :success, :user_id
end
但Rails的魔術不會在所有的工作:
rails console
user = User.create(email: '[email protected]', password: 'test', password_confirmation: 'test', first_name: 'Test', last_name: 'Test', date_of_birth: '1992-12-21 00:00:00.000000', gender: 'male')
feature = user.feature.create(content: 'First Feature', hits: 5, icon: 'icon-bell', images: null, lable: 'Bell', rank: 1, success: 3)
返回等錯誤:
NoMethodError: undefined method `feature' for #<User:0x4b377b8>
我讀了很多帖子,但我找不到任何錯誤。有人能給我一個提示,我認爲它是我犯的這樣一個愚蠢的錯誤。謝謝!
你大概的意思'features',不特徵。用戶有很多*功能*。所以調用user.features.create() – calas