RailsCasts Pro episodes are now free!

Learn more or hide this

John's Profile

GitHub User: nguyenj

Site: http://jnguyen.me/

Comments by John

Avatar

Can you show an example without using Virtus?

Avatar

I'm on rails4 ruby2.1 and I can't seem to get the tagging table to record the association. Any help will be much appreciated. (doing this from scratch)

ruby
# records.rb

has_many :taggings
has_many :tags, through: :taggings


def tag_list
  tags.map(&:name).join(", ")
end
  
def tag_list=(names)
  self.tags = names.split(",").map do |n|
    Tag.where(name: n.strip).first_or_create!
  end
end
ruby
# tagging.rb

belongs_to :record
belongs_to :tag
ruby
# tag.rb

has_many :taggings
has_many :records, through: :taggings
ruby
# views/records/_form.html.erb

<div class="field">
  <%= f.label :tag_list %><br />
  <%= f.text_field :tag_list %>
</div>
ruby
# records_controller.rb

def record_params
  params.require(:record).permit(:name, :tag_list)
end

The tags do get created in the tag table; but no association defined in the tagging table between the record and the tag.

Avatar

How did you deal with your form post? Was it to events_comments_path or events_pictures_comments_path?