Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:48:19+00:00 2026-06-10T02:48:19+00:00

I have a author model class Author include Mongoid::Document field :name end In My

  • 0

I have a author model

class Author
  include Mongoid::Document
  field :name

end

In My article form i want to bring all authors

 <div class="field">
    <%= f.label :author_tokens, "Authors" %><br />
    <%= f.text_field :author_tokens, "data-pre"=> @article.authors.map(&:attributes).to_json %>
  </div>

It is working. all authors name are seen. now i want to submit all author name i click inside Authors. what should be my article model for this? I am confuse. Here when i publish

{"utf8"=>"✓",
 "authenticity_token"=>"bE0PpLx+qBUJqIavfvpDOjzrhIHFku+IrgjnU0OLOC8=",
 "article"=>{"name"=>"ram",
 "published_on(1i)"=>"2012",
 "published_on(2i)"=>"8",
 "published_on(3i)"=>"20",
 "author_tokens"=>"",
 "content"=>"fdsfds"},
 "commit"=>"Create Article"}

author_tokens field is empty.
I have my article model

class Article

  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes
  field :name
  field :content
  field :author_tokens
  field :token_inputs
 field :published_on, :type => Date 
 validates_presence_of :name,:content

has_many :authors
 attr_reader :author_tokens

 def author_tokens=(ids)
 self.author_ids =ids.split
 end

end

What should be my article model so that i can save all input author tokens name in my article collection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:48:20+00:00Added an answer on June 10, 2026 at 2:48 am

    On the Article model, try removing:

    field :author_tokens
    

    and adding:

    attr_accessible :author_tokens
    

    Also, not sure where the token_inputs field is getting used. Maybe unnecessary?

    EDIT:

    I overlooked this earlier, but you need has_and_belongs_to_many on both sides of the relation for this to even work the way you want, so:

    Class Author
      has_and_belongs_to_many :articles
      ...
    end
    

    and:

    Class Article
      has_and_belongs_to_many :authors
      # has_many :authors <- Remove this
      ...
    end
    

    To clarify my original explanation:

    1) The setter method you wrote for author_tokens and the attr_reader : author_tokens are both fine, but if you’re using mass assignment in the controller (likely), you need to make the author_tokens attribute mass assignable with attr_accessible :author_tokens. Mongoid might do this automatically if you haven’t explicitly set anything else as attr_accessible yet, depending on what version you’re using.

    2) You shouldn’t need the field :author_tokens line since it’s a virtual attribute accessed via the setter you wrote, and the attr_reader call. You don’t actually want to store the value the user passes into author_tokens in the DB, you want the setter to put those values into the author_ids field for you.

    3) The has_and_belongs_to_many :authors call will have created the author_ids field in the document for you.

    4) Assuming you’re using the pattern shown here, the front-end implementation should be no different when using Mongoid instead of ActiveRecord.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this setup in my models: class Author(models.Model): name = models.CharField(max_length=100) class Topic(models.Model):
I have a form that has a choice list: <select name=cellSerpro id=idcellserpro class=field text
I have the following two models class Author(Model): name = CharField() class Publication(Model): title
If I have two models like class Author(models.Model): name = models.CharField(max_length=100) title = models.CharField(max_length=3,
I have this sample model working with the admin class Author(models.Model): name = models.CharField(_('Text
Let's say I have some contrived models: class Author(Model): name = CharField() class Book(Model):
Here's an example: If I have these classes class Author(models.Model): name = models.CharField(max_length=45) class
I have the following models: class Author(db.Model): name = db.StringProperty() class Story(db.Model): author =
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.