Ruby on Rails项目中的未定义错误

时间:2012-08-14 22:00:52

标签: ruby-on-rails class

我对Rails很陌生,而且我遇到了这个问题。我猜你对专业人士来说会比较简单。我有一个名为User的模型,它包含所有用户属性。我还创建了一个名为list的模型,现在我想。我现在正试图从用户那里调用方法创建购买下面这样的事情(所有发生在控制台中)

sample = User.create(#attributes here)
newlist = sample.List.create(#attributes here)

然后我收到此错误

irb(main):011:0> sample.Lists.new
NoMethodError: undefined method `Lists' for #<User:0x4146750>

以下是我的用户和列表的模型文件

# == Schema Information
#
# Table name: users
#
#  id                 :integer          not null, primary key
#  firstName          :string(255)
#  middleName         :string(255)
#  lastName           :string(255)
#  email              :string(255)
#  facebookexternalId :integer
#  userType           :integer
#  gender             :string(255)
#  description        :string(255)
#  location           :string(255)
#  image              :string(255)
#  password           :string(255)
#  notificationId     :string(255)
#  disabled           :boolean
#  disabledNotes      :string(255)
#  city               :string(255)
#  country            :string(255)
#  joinDate           :string(255)
#  created_at         :datetime         not null
#  updated_at         :datetime         not null

class User < ActiveRecord::Base

  attr_accessible :firstName, :middleName , :lastName ,:email , :facebookexternalId, :gender , :description, :location , :image , :city, :country, :disabled
   email_regex= /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
   validates :firstName , :presence =>true,
                          :length => {:maximum => 45}
   validates :lastName , :presence =>true,
                          :length => {:maximum => 45}
   validates :email , :presence =>true,
                     :format =>{:with => email_regex},
                     :uniqueness => {:case_sensitive => false}
  validates :description, :length => {:maximum => 140}

  has_many :lists
end

解释

# == Schema Information
#
# Table name: lists
#
#  id          :integer          not null, primary key
#  name        :string(255)
#  user_Id     :integer
#  active      :boolean
#  type        :string(255)
#  description :string(255)
#  roughList   :boolean
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#

class List < ActiveRecord::Base
  belongs_to :user
end

1 个答案:

答案 0 :(得分:3)

你应该使用列表中的复数形式,例如,

sample_user.lists.create ...

sample_user.lists.new

就像你在:has_many

中命名它一样