ActiveRecord setting has_many association

Hi,

Can someone help me with this, why does it not work when the association
is already set, shouldn’t it be overridden?

require ‘rubygems’
require ‘activerecord’

ActiveRecord::Base.establish_connection(:adapter => ‘sqlite3’, :database
=> ‘:memory:’)

class Rider < ActiveRecord::Base
connection.create_table :riders, :force => true do |t|
t.string :name
end

has_many :starts
end

class Start < ActiveRecord::Base
connection.create_table :starts, :force => true do |t|
t.string :class_no
t.integer :rider_id
end

belongs_to :rider
end

Why is not this true?

other_rider = Rider.create
start = Start.new(:rider => other_rider)
rider = Rider.create(:starts => [start])
puts rider.starts.reload.size == 1 # => false

This works

rider = Rider.create(:starts => [Start.create])
puts rider.starts.reload.size == 1 # => true

And this

rider = Rider.create(:starts => [Start.create(:rider_id =>
Rider.create.id)])
puts rider.starts.reload.size == 1 # => true

Thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs