ruby on rails - Creating assoc record in HABTM through -
models:
cities.rb:
has_many :cities_users has_many :users, :through => :cities_users
i have habtm (through) between cities
, users
. want view cities associated user. here's have , error is:
users.rb
has_many :cities_users has_many :cities, :through => :cities_users
controller:
@user = user.find(current_user.id) @users_cities = @user.cities
i have written migration creates join table:
create_table "cities_users", :id => false, :force => true |t| t.integer "user_id" t.integer "city_id" end
this error (relating second line of controller code):
uninitialized constant user::citiesuser
i'm having similar problems creating city associated user too.
many thanks.
you should create new model if want use has_many :through
association.
please, consider using has_and_belongs_to_many
direct many-to-many connection no intervening model.
for details can read http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many.
Comments
Post a Comment