Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I format tags using acts-as-taggable-on gem before saving my user model?
    primarykey
    data
    text
    <p>How can I format tags using acts-as-taggable-on before saving the model?</p> <p>I am using the following gem in my ruby on rails project</p> <pre><code>gem 'acts-as-taggable-on', '~&gt; 2.3.1' </code></pre> <p>I have added interests tags to my user model.</p> <p>user.rb</p> <pre><code>class User &lt; ActiveRecord::Base acts_as_ordered_taggable_on :interests scope :by_join_date, order("created_at DESC") rolify # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :interest_list # this does not work before_save :clean_my_tags # this also does not work # before_validation :clean_my_tags def clean_my_tags if @interest_list # it appears that at this point, @interest_list is no longer a string, but an # array of strings corresponding to the tags @interest_list = @interest_list.map {|x| x.titleize} end end end </code></pre> <p>Suppose that the last user already has the interest tags: basketball, golf, soccer If I change the interests like</p> <pre><code>u = User.last u.interest_list = "cats, dogs, rabbits" u.save </code></pre> <p>Then u.interest_list will be ["cats", "dogs", "rabbits"]</p> <p>However, u.interests will remain an array of tag objects associated with basketball, golf, soccer</p> <p>How can I make sure that the tags are formatted before tags are saved?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload