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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/R8mT37V5SYUJ. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Nov-17 11:10 UTC
Re: Help needed for error in foreign key validation
On Nov 17, 10:19 am, rubyrookie <asajn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 :countryYour 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Thanks worked. On Saturday, November 17, 2012 3:11:33 AM UTC-8, Frederick Cheung wrote:> > > > On Nov 17, 10:19 am, rubyrookie <asajn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/a005mO4O7ecJ. For more options, visit https://groups.google.com/groups/opt_out.