Ruby on Rails Tuesday, March 27, 2018



On Tuesday, March 27, 2018 at 11:44:54 AM UTC-4, Walter Lee Davis wrote:

> On Mar 27, 2018, at 9:20 AM, fugee ohu <fuge...@gmail.com> wrote:
>
> Actually the first thing is see when I visit that link is the use of validate without the s

The validate method is used along with a custom validator method. The validates method is "built in" and uses a DSL to specify how you want the validations to work.

validate :has_a_bow_on_top?

private

def has_a_bow_on_top?
  unless bow&.on_top?
    errors[:bow].add "must be on top"
    false
  end
end

The built-in validates method is configured like this:

validates :bow, presence: true

This is a very basic example, there are lots more things you can do with it. For all of our sakes, please read this entire page: http://guides.rubyonrails.org/active_record_validations.html

Walter

>
> On Tuesday, March 27, 2018 at 6:54:34 AM UTC-4, Allen Maxwell wrote:
> It might help to have the models for auction and user... also, I like to add the annotate gem which gives some header comments in all my models to show what fields etc are there.  Very helpful info and references.
>
> you already know that the validate needs to be validates...
>
> is this an active system or SUD?  do you have a solid development database that is well populated to test/develop against?
>
> this link may help some:  http://guides.rubyonrails.org/active_record_validations.html#custom-methods
>
> other than that I'd have to see it in action to debug further.
>
> Good luck
>
> Max
>
> On Monday, March 26, 2018 at 6:17:56 PM UTC-6, fugee ohu wrote:
> This model has some errors that I wasn't able to get sorted out the two lines that begin with validate should be validates but after that I don't understand what the author's trying to do If I change them to validates I get the error Unknown validator: 'MessageValidator' for   validate :auction_active?, message: :auction_active
>
>
> class Bid < ActiveRecord::Base
>   belongs_to :auction
>   belongs_to :user
>
>   validates :auction, :user, presence: true
>   validate :auction_active?, message: :auction_active
>   validate :last_user_different?
>
>   scope :sorted, -> { order(:created_at) }
>
>   after_create :update_auction
>
>   private
>
>   def auction_active?
>     if auction && !auction.active?
>       if auction.finished?
>         errors.add(:auction, :finished)
>       else
>         errors.add(:auction, :not_started)
>       end
>     end
>   end
>
>   def last_user_different?
>     if auction && user
>       errors.add(:user, :same_user_twice) if auction.last_user == user
>     end
>   end
>
>   def update_auction
>     auction.increase_price_and_time
>     auction.publish_updates
>   end
> end
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b8ab4f9b-1682-4006-8d91-8f2c556f34e6%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


Thanks Walter And passing a symbol to message: how does that work? 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/53605b52-f944-48fe-9a04-54a2b629d9a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment