Rails NoMethodError: undefined method `valid?' -
i relatively new rails , can't figure out how fix bug. here models:
user
class user < activerecord::base before_save { email.downcase! } has_many :tickets has_many :events, through: :tickets validates :first_name, presence: true validates :last_name, presence: true valid_email_regex = /\a[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255}, format: { with: valid_email_regex }, uniqueness: { case_sensitive: false } end
event:
class event < activerecord::base has_many :tickets has_many :users, through: :tickets validates :event_title, presence: true validates :event_place, presence: true validates :event_description, presence: true validates :event_date, presence: true validates_numericality_of :event_number_tickets, presence: true, numericality: { only_integer: true }, greater_than: 0 end
ticket:
class ticket < activerecord::base belongs_to :user belongs_to :event end
i have test file tickets
require 'test_helper' class tickettest < activesupport::testcase def setup @user = user.new(first_name: "example", last_name: "user") @event = event.new(event_title: "example event", event_place: "123 example ave.", event_description: "our example event children introduced technology", event_date: datetime.now, event_number_tickets: 30) @ticket = ticket.new(event: @event, ticket_type: "regular", number_of_children: 1) @user.tickets << @ticket end test "should valid" assert @user.tickets.valid? end end
whenever run test following error:
nomethoderror: undefined method `valid?' #<ticket::activerecord_associations_collectionproxy:0x007fae767617c8> test/models/ticket_test.rb:18:in `block in <class:tickettest>'
do have create custom valid method particular example.
there's not valid?
method activerecord collection. if you're trying test last ticket created valid, can assert @ticket.valid?
.
i think might doing bit work in setup method. try setup user , event model in setup, , break rest of described different test blocks.
Comments
Post a Comment