我对 Rails 很陌生,我偶然发现了这个问题。我猜这对你们专业人士来说会相对简单。我有一个名为 User 的模型,其中包含所有用户属性。我还创建了一个名为 list 的模型,现在我想要。我现在正在尝试从用户购买中调用 create 方法,执行以下操作(所有操作都发生在控制台中)
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