Помогите с ассоциациями

0 views
Skip to first unread message

GeniyZ

unread,
Nov 20, 2008, 2:04:47 AM11/20/08
to RubyOnRails to russian
как быть:
если несколько значений таблицы - являются кортежами другой таблицы.

create_table :o_types do |t|
t.integer :group
t.string :name
end



create_table :rooms do |t|
t.integer :material # -> o_types
t.integer :quality, :null => false # -> o_types
t.integer :balcony, :null => false # -> o_types
t.integer :window_material, :null => false # -> o_types
t.integer :condition # -> o_types
t.integer :price, :commission, :null => false
........
end

Когда сделал так:


class Room < ActiveRecord::Base
belongs_to :deal, :class_name=>'OType', :conditions=>'group=
(select id from o_types where "name" = "Deal")'
belongs_to :material, :class_name=>'OType', :conditions=>'group=
(select id from o_types where "name" = "Material")'
belongs_to :quality, :class_name=>'OType', :conditions=>'group=
(select id from o_types where "name" = "Quality")'
belongs_to :balcony, :class_name=>'OType', :conditions=>'group=
(select id from o_types where "name" = "Balcony")'
................
end

долго думал, что прописать в моделе OType, прописал просто

has_one :room

При добавлении вываливается с ошибкой:

OType(#37299550) expected, got String(#20902930)

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/
associations/association_proxy.rb:210:in `raise_on_type_mismatch'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/
associations/belongs_to_association.rb:22:in `replace'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/
associations.rb:1114:in `window_material='
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/
base.rb:2372:in `send'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/
base.rb:2372:in `attributes='
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/
base.rb:2371:in `each'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/
base.rb:2371:in `attributes='
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/
base.rb:2141:in `initialize'
app/controllers/rooms_controller.rb:45:in `new'
app/controllers/rooms_controller.rb:45:in `create'

Ему не нравится, что window_material - String. Как быть, подскажите,
ссылку на мануал укажите. С ассоциациями у меня как-то неочень ...
Reply all
Reply to author
Forward
0 new messages