On Jueves, 6 de Mayo de 2010 10:57:06 dlamet escribió:
> I have an app with users that can optionally have stock certificates
> and offers to buy or sell. I have validations for these objects (see
> model classes below). When I create a new user in AS, even though I
> do not specifically add a certificate or offer, I get validation
> errors for both, as if it's trying to create empty associated objects.
> If I remove the validations, it does indeed create erroneous
> certificates and offers. Why is AS doing this? Thanks.
>
> (I was able to reproduce this in AS 1.2, then upgraded to latest 2.2
> on Ubuntu and Mac OS X 10.5.8.)
Are you using rails-2.3 or master branch? Is it updated? AS should not create
empty objects, but you can disable show blank record as a workaround.
>
> =================================================
> class User < ActiveRecord::Base
> belongs_to :user_status
> has_and_belongs_to_many :roles
> has_many :user_contacts
> has_many :certificates
> has_many :offers
>
> require "digest/sha1"
>
> validates_length_of :first_name, :in => 2..12, :allow_nil => false
> validates_length_of :last_name, :in => 2..16, :allow_nil => false
> validates_presence_of :login
> validates_uniqueness_of :login
> validates_length_of :login, :in => 4..16
> validates_presence_of :password
> validates_confirmation_of :password
> validates_length_of :address, :in => 4..128, :allow_nil => false
> validates_length_of :city, :in => 4..32, :allow_nil => false
> validates_length_of :zip_code, :in => 5..10
>
> acts_as_reportable
>
> before_save :doBeforeSave
>
> def name
> "#{first_name} #{last_name}"
> end
>
> def doBeforeSave
> self.password = Digest::SHA1.hexdigest(self.password)[0..39]
> unless (self.password.length > 20)
> end
>
> def confirm_password=(sNewPassword)
> @sConfirmPassword = sNewPassword
> end
>
> def confirm_password
> @sConfirmPassword
> end
>
> def getEmail
> emailType = ContactType.find(:first, :conditions => ["name = ?",
> "eMail"])
> UserContact.find(:first, :conditions => ["user_id = ? AND
> contact_type_id = ?", self.id, emailType ])
> end
>
> def getPhone
> phoneTypes = ContactType.find(:all, :conditions => ["name IN (?)",
> ContactType.getPhoneTypes])
> UserContact.find(:first, :conditions => ["user_id = ? AND
> contact_type_id IN (?)", self.id, phoneTypes ])
> end
>
> end
>
> =================================================
> class Certificate < ActiveRecord::Base
> belongs_to :user
> belongs_to :certificate_state
> has_and_belongs_to_many :offers
>
> validates_presence_of :number
> validates_uniqueness_of :number, :message => "That certificate
> number is already registered with another investor."
> validates_numericality_of :number_of_shares, :only_integer => true
>
> def to_s
> "#{number}: #{number_of_shares} shares"
> end
>
> end
>
> =================================================
> class Offer < ActiveRecord::Base
> belongs_to :user
> belongs_to :offer_state
> belongs_to :offer_type
> has_and_belongs_to_many :certificates
>
> validates_presence_of :user_id
> validates_presence_of :offer_type_id
> validates_presence_of :offer_state_id
> validates_numericality_of :number_of_shares, :only_integer =>
> true, :greater_than_or_equal_to => 0, :less_than_or_equal_to =>
> 10000000
> validates_presence_of :price_per_share
> def validate
> errors.add_to_base "There must be at least 30 days between date
> opened and closed" if self.date_closed < (self.date_opened + 30.days)
> end
>
> def to_s
> "#{offer_type.name} #{number_of_shares} at $#{"%.2f" %
> price_per_share} per share."
> end
>
> end
--
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) [email protected]
--
You received this message because you are subscribed to the Google Groups
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/activescaffold?hl=en.