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

  • Home
  • SEARCH
  • 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 7555001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:29:13+00:00 2026-05-30T11:29:13+00:00

How can I return errors messages from a cross reference table with multiple records

  • 0

How can I return errors messages from a cross reference table with multiple records when I trying to create those? I’m trying this:

## activity_set.rb
class ActivitySet < ActiveRecord::Base
  has_many :activity_set_lessons
  has_many :lessons, :through => :activity_set_lessons

  validates :name, :presence => true

  def activity_set_lessons=(data)
    data.each_with_index do |v, i|
      activity_set_lessons.build(
        :lesson_id  => v[:lesson_id],
        :sort_order => i,
        :weight_percentage => v[:weight_percentage]
      )
    end
  end
end

## activity_set_lesson.rb
class ActivitySetLesson < ActiveRecord::Base
  belongs_to :activity_set
  belongs_to :lesson

  validates :lesson_id, :presence => true
  validates_each :weight_percentage do |record, attr, value|
    record.errors.add :base, "woot" if value.blank?
  end
end

This is the request data:

## params[:activity_set]
"activity_set" => {
  "name" => "hshshshs", 
  "keywords" => "", 
  "activity_set_lessons" => [
    {"weight_percentage" => "", "lesson_id"=>"4"}, 
    {"weight_percentage" => "", "lesson_id"=>"5"}
  ]
}

Error messages from @activity_set when I do #save:

{
  "errors":{
    "activity_set_lessons":["is invalid","is invalid"]
  },
  "full_messages":[
     "Activity set lessons is invalid","Activity set lessons is invalid"
  ]
}

I always got the same error message even if I’m adding a custom one in the join table. How can I return a message like: “woot 1 is wrong” or something like that, per validation?.

Thanks.

  • 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-05-30T11:29:14+00:00Added an answer on May 30, 2026 at 11:29 am

    make use of accepts_nested_attributes_for

    ## activity_set.rb
    class ActivitySet < ActiveRecord::Base
      has_many :activity_set_lessons
      has_many :lessons, :through => :activity_set_lessons
    
      validates :name, :presence => true
    
      accepts_nested_attributes_for :activity_set_lessons
    end
    

    view will look like

    = form_for @activity_set do |f|
      [activity_set form fields ]
      = f.fields_for :activity_set_lessons do |p|
        = p.select :lession_id
        = p.select :weight_percentage
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a description of the various errors that NSXMLParser can return? I'm aware
can someone please help me. why does this return an error: Dim stuff As
How can return a subset of a FormCollection with key/values in tact for those
How can return a variable from a thead (I have the threads handle too).
SQL Server procedure can return result sets. I have a table emp(emp__id, emp__name, ...)
How can i get IIS 7 to return my custom error pages for file
This code returns an error: AttributeError: can't set attribute This is really a pity
What query can return the names of all the stored procedures in a SQL
In c# you can return an integer to the underlying caller using Environment.Exit(n) (which
I know that I can return the index of a particular character of a

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.