Re: Help needed for error in foreign key validation

11 views
Skip to first unread message

Frederick Cheung

unread,
Nov 17, 2012, 6:10:43 AM11/17/12
to Ruby on Rails: Talk


On Nov 17, 10:19 am, rubyrookie <asajn...@gmail.com> wrote:
>
>
>  Can you tell me how to fix the issue? If I remove the
>  validates_presence_of :country I get a database error as expected when i
> update bank table with a country id not in country table.
>
> class Bank < ActiveRecord::Base
>
>   attr_accessible :name,:country_id
>
>   has_one :country

Your association is wrong - if bank has a country_id column then the
association should be a belongs_to

Fred


>
>   validates :name, :presence => true
>
>    validates_presence_of :country
>
> end
>
> class Country < ActiveRecord::Base
>
>   attr_accessible :name
>
> end

rubyrookie

unread,
Nov 17, 2012, 11:54:51 AM11/17/12
to rubyonra...@googlegroups.com
Thanks worked.
Reply all
Reply to author
Forward
0 new messages