0

モデルを持っている

Class ModelA < ActiveRecord::Base
  has_many :apples
  before_save :include_prime_apple_in_apples
  has_one :prime_apple


 def include_prime_apple_in_apples
     self.apple_ids << prime_apple_1.id
 end
end

l=ModelA.new(:apple_ids => [ "ap_1_id", "ap_2_id"],:prime_apple => prime_apple_1)
l.save
l.apple_ids.should include(prime_apple_1.id) # this doesnt seem to work

アソシエーションに渡されたパラメータをどのように変更しますか?

4

1 に答える 1

0

そこには何か問題がありますが、あなたの質問に答えるために:私の知る限り、「other_ids」にのみ割り当てることができます。has_many リレーションに直接プッシュすることもできます:

self.apple_ids = self.apple_ids + [prime_apple_1.id]

また

self.apples << prime_apple_1

Appleモデルに別のforeign_keyが設定されていますか? has many will do a select * from apples where modela_id = X has one will do select * from apples where modela_id = X limit 1 つまり、prime_apple アクセサーに設定するものは何でも、最初の Apple レコードが返されます。 ..

于 2012-05-18T19:33:34.647 に答える