Search code examples
ruby-on-railsbelongs-to

Ruby on Rails, belongs_to does not work


I have the following relationships:

class GroupsNorm < ActiveRecord::Base
   attr_accessible  :group_name, :file_names
   has_many :platformas
end

class Platforma < ActiveRecord::Base
  attr_accessible :file_name, :norm_type, :groups_norm_id
  belongs_to :groupsnorm

end

In the console:

a=Platforma.new(:file_name=>"kkkkk.cel",:groups_norm_id=>9)
a.save
b=GroupsNorm.find(9)

=> #<GroupsNorm id: 9, group_name: "aaaaaa", file_names: "/system/Files/aaaaaa_cel_files...", created_at: "2013-05-20 13:37:14", updated_at: "2013-05-20 13:37:14">

b.platformas

[#<Hgu133a id: 1, groups_norm_id: 9, file_name: "aaa.cel", created_at: "2013-05-20 21:31:05", updated_at: "2013-05-20 21:31:05">, #<Hgu133a id: 2, groups_norm_id: 9, file_name: "kkkkk.cel", created_at: "2013-05-20 21:47:24", updated_at: "2013-05-20 21:47:24">, #<Hgu133a id: 3, groups_norm_id: 9, file_name: "kkkkk.cel",created_at: "2013-05-20 21:54:37", updated_at: "2013-05-20 21:54:37">]

So, everything is all right, and our new record is as well there. But when I call:

irb(main):011:0> a.groupsnorm
=> nil

Why doesn't it show me the GroupsNorm record that has id==9 ??

Thanks in advance


Solution

  • Try belongs_to :groups_norm I Think that should work for you.

    More info on ActiveRecord associations:

    http://guides.rubyonrails.org/association_basics.html