Forum: Ruby on Rails Help needed for error in foreign key validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5c5b50426affe847d550a34ff9eae507?d=identicon&s=25 rubyrookie (Guest)
on 2012-11-17 12:09
(Received via mailing list)
I have two models bank and country. User should only associate a  Bank
with
a country id present in the country table and I put validates presence
of
country to enforce it but i get error mysql2::Error: Unknown column
'countries.bank_id' in 'where clause': SELECT `countries`.* FROM
`countries` WHERE `countries`.`bank_id` = 17 LIMIT 1 when updating the
bank
table



 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

  validates :name, :presence => true



   validates_presence_of :country

end





class Country < ActiveRecord::Base

  attr_accessible :name





end
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-11-17 12:11
(Received via mailing list)
On Nov 17, 10:19am, rubyrookie <asajn...@gmail.com> wrote:
>  has_one :country
Your association is wrong - if bank has a country_id column then the
association should be a belongs_to

Fred
5c5b50426affe847d550a34ff9eae507?d=identicon&s=25 rubyrookie (Guest)
on 2012-11-17 17:56
(Received via mailing list)
Thanks worked.
This topic is locked and can not be replied to.