Why doesn't Rails automatically create join table entries?

I have a simple has_many setting through relationships:

class Tag < ActiveRecord::Base
  has_many :profile_tags
  has_many :profiles, :through => :profile_tags
end

class ProfileTags < ActiveRecord::Base
  belongs_to :profile
  belongs_to :tag
end

class Profile < ActiveRecord::Base
  has_many :profile_tags
  has_many :tags, :through => :profile_tags
end

In my opinion, I accept a set of tags (only strings) and repeat them in my controller and call Tag.create (...) on each of them and pushing them into an array. All of this works great.

So, I get to the point where I have the tag array objects (tags) that were returned by the call to create, and the @profile variable created using Profile.new

I'd like to do: @profile.tags = tags

This causes this error on the line where I am trying to execute the assignment:

uninitialized constant Profile::ProfileTag

Rails , , http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association , , , , .

, ?

+3
1

Rails , , .. ProfileTags ProfileTag.

, Rails , , - script/destroy script/generate Rails 2.x rails destroy rails generate Rails 3.

, :class_name => 'ProfileTags' has_many .

+4

Source: https://habr.com/ru/post/1764311/


All Articles