Validates, what's wrong?

class Category < ActiveRecord::Base
belongs_to :sector
has_and_belongs_to_many :suppliers
validates :sector, :name, :presence => true

As you see I have validates :sector, :presence => true.
When I create a new Category, if I don’t select a sector I have an
error page with the message:

Couldn’t find Sector with ID=

But there is the validates, why I don’t have the message sector can’t be
blank?

On Wed, Feb 23, 2011 at 5:37 PM, Mauro [email protected] wrote:

class Category < ActiveRecord::Base
belongs_to :sector
has_and_belongs_to_many :suppliers
validates :sector, :name, :presence => true

As you see I have validates :sector, :presence => true.
When I create a new Category, if I don’t select a sector I have an
error page with the message:

I don’t know about the others but when associating other tables like
this, I
don’t do the validations
this way.

i create a method that i can call with validates.

validates :sector_should_not_be_blank

def sector_should_not_be_blank
errors.add(:sector, “can’t be blank”) if sector.nil?
end

[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On 23 February 2011 09:37, Mauro [email protected] wrote:

class Category < ActiveRecord::Base
belongs_to :sector
has_and_belongs_to_many :suppliers
validates :sector, :name, :presence => true

As you see I have validates :sector, :presence => true.
When I create a new Category, if I don’t select a sector I have an
error page with the message:

Couldn’t find Sector with ID=

Is that error occurring before the save happens, or are you saying
that it has saved it with no sector?

Colin

On 23 February 2011 11:43, Colin L. [email protected] wrote:

Couldn’t find Sector with ID=

Is that error occurring before the save happens, or are you saying
that it has saved it with no sector?

I had an error in the controller sorry.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs