Note that there are some explanatory texts on larger screens.

plurals
  1. POHave user add "tags" while adding article with Mongoid & Ruby on Rails
    primarykey
    data
    text
    <p>Im using Ruby on Rails to allow a user to add a project post much like Stackoverflow. I can do this with the regular MySQL database, but I am unsure as to how it works with Mongoid.</p> <p>This is how the process works:</p> <ol> <li>User writes some details about the project (client, date, description)</li> <li>Add tags like Stackoverflow, where they just simply need to add a space between each one.</li> <li>Submit the post</li> </ol> <p>Now in my model I try to break the tags up into an array (splitting where there is a space) and then saving the tags one after the other. However, the row for the Project and the Tag do not reference one another. The Project tag_ids = [] and the Tag project_ids = []</p> <p>project.rb model</p> <pre><code>class Project include Mongoid::Document include Mongoid::MultiParameterAttributes field :client, :type =&gt; String field :description, :type =&gt; String field :url, :type =&gt; String field :project_date, :type =&gt; Date has_and_belongs_to_many :tags attr_accessor :tag_names after_save :assign_tags def tag_names @tag_names || tags.map(&amp;:name).join(" ") end def assign_tags @project = self @project_id = self.id if @tag_names self.tag_names = @tag_names.split(/\s+/).map do |name| Tag.find_or_create_by(:name =&gt; name) end end end end </code></pre> <p>tag.rb model</p> <pre><code>class Tag include Mongoid::Document field :name, :type=&gt; String has_and_belongs_to_many :projects end </code></pre> <p>Any ideas as to how to add these reference ids? Thanks!</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.
 

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