#1367 ✓resolved
Kevin Watt

belongs_to default :require validation not called unless associaton is used

Reported by Kevin Watt | July 17th, 2010 @ 01:36 AM | in 1.1

class Ban
include DataMapper::Resource property :id, Serial property :ip, String

property :until, DateTime, :required => true
property :reason, Text, :length => 20..9999, :required => true
property :created_at, DateTime

property :affected, Yaml, :default => {}
property :hits, Integer, :default => 0

#property :sitewide, Boolean # always are?
property :last_hit, DateTime

#property :user_id, Integer
belongs_to :user # required by default

has n, :users, :through => Resource # subjects, need2 create end

b = Ban.new
=> # ree-1.8.7-2010.02 > b.e
=> #["Until must not be blank"]}> ree-1.8.7-2010.02 > b.until = Time.now
=> Fri Jul 16 23:26:30 -0700 2010 ree-1.8.7-2010.02 > b.e
=> # ree-1.8.7-2010.02 > b.save -> works

ree-1.8.7-2010.02 > b.user
=> nil ree-1.8.7-2010.02 > b.e
=> #["User must not be blank"]}>

The default :required on belongs_to is only enforced once you try to get the valute for that association.

Comments and changes to this ticket

  • Kevin Watt

    Kevin Watt July 17th, 2010 @ 11:35 AM

    I should mention b.e is a helper method I mixed in to allow b.valid?; b.errors.to_hash, as I always wished valid? would return the errors rather than just true/false.

    After posting this, I suspected this was an artifact of the rails console strange auto-reloading. But I've tested it in a script as well and it is still not working (actually doesn't validate user even after I call it once).

    This seems like a pretty large / odd bug, I'm wondering if others will be able to reproduce it.

  • Jonathan Stott (namelessjon)

    Jonathan Stott (namelessjon) July 17th, 2010 @ 01:26 PM

    • State changed from “new” to “unconfirmed”

    I think this is caused by a failure to call DataMapper.finalize after all models are declared. This takes care of initializing all relationship properties. Could you verify you're calling finalize first?

  • Kevin Watt

    Kevin Watt July 17th, 2010 @ 01:48 PM

    I believe you're right - a simpler test with finalize worked, but my actual code test with finalize still isn't working, so I'll do some more looking at it and get back.

    Can you call finalize more than once?

  • Jonathan Stott (namelessjon)

    Jonathan Stott (namelessjon) July 18th, 2010 @ 01:55 PM

    Yes, finalize is safe to call multiple times.

  • Kevin Watt

    Kevin Watt July 18th, 2010 @ 02:35 PM

    Then we should add a call to finalize in whatever railtie is loaded from rails console...

    Though, I guess it would be irritating if it prevented being able to load the console at all... I'd rather it just show the error, but then put you in the console anyways, let you try to fix it, then call finalize again yourself.

  • Martin Gamsjaeger (snusnu)
  • felix

    felix November 11th, 2010 @ 01:50 AM

    I think it still is an issue, at least it is for me! Even after calling DataMapper.finalize explicitly I cannot get this requirement to invalidate.

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) November 11th, 2010 @ 04:37 AM

    • State changed from “unconfirmed” to “resolved”
    • Milestone set to 1.1
    • Milestone order changed from “196240” to “0”

    I couldn't reproduce this problem with this standalone: https://gist.github.com/672322 and latest DM from master, so I guess we're good.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »