Spyros
Spyros

Reputation: 48606

Create_Association does not work

it's been quite some time since i last wrote Rails and i've forgotten some stuff i guess :P I'm having a simple has_many through but i can't get create_association to work for some reason. The models are :

class City < ActiveRecord::Base
    has_many :city_buildings
    has_many :buildings, :through => :city_buildings

class CityBuilding < ActiveRecord::Base
    belongs_to :city
    belongs_to :building
end

I write something like :

c = City.first
c.create_building

and i get :

NoMethodError: undefined method `create_building' for #<City:0x007f9a8f9494f0>

Why do i get that ? Any idea ?

Upvotes: 3

Views: 810

Answers (1)

jibiel
jibiel

Reputation: 8303

c = City.first
c.buildings.create :attribute => 'value'

Methods which the has_many declaration adds to your models.

Upvotes: 7

Related Questions