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 3403930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:19:13+00:00 2026-05-18T05:19:13+00:00

In rails 2.3.8 I am having trouble with validations on a accepts_nested_attributes_for association if

  • 0

In rails 2.3.8 I am having trouble with validations on a accepts_nested_attributes_for association if I loop through or even inspect the habtm association in validation the lessons are loaded and any updates are lost. For example, I have the following schema:

ActiveRecord::Schema.define(:version => 20100829151836) do

 create_table "attendees", :force => true do |t|
  t.integer  "lesson_set_id"
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
 end

 create_table "lesson_sets", :force => true do |t|
  t.datetime "created_at"
  t.datetime "updated_at"
 end 

 create_table "lesson_sets_lessons", :id => false, :force => true do |t|
  t.integer "lesson_id"
  t.integer "lesson_set_id"
 end

 create_table "lessons", :force => true do |t|
  t.integer  "activity_id"
  t.integer  "limit"
  t.datetime "created_at"
  t.datetime "updated_at"
 end
end

and the following models:

class Attendee < ActiveRecord::Base
 belongs_to :lesson_sets
end
class Lesson < ActiveRecord::Base
 has_and_belongs_to_many :lesson_sets
end
class LessonSet < ActiveRecord::Base
 has_and_belongs_to_many :lessons
 has_many :attendees
 accepts_nested_attributes_for :lessons
 validate do |lesson_set|
  lesson_set.lessons.each do |l| 
   if l.limit < attendees.count
    self.errors.add_to_base("Sorry only #{l.limit} people allowed on lesson")
   end
  end
 end
end

If I call the following:

>> ls = LessonSet.first
=> #<LessonSet id: 1, created_at: "2010-08-29 07:50:23", updated_
at: "2010-08-29 07:50:23">
>> Lesson.last
=> #<Lesson id: 1, activity_id: 29, created_at: "2010-08-29 07:57:13", updated_a
t: "2010-08-29 17:04:34", limit: 5>
params = {"lessons_attributes"=>{"0"=>{"activity_id"=>"3", "id"=>"1", "_destroy"=>""}}, "id"=>"1"}
=> {"id"=>"1", "lessons_attributes"=>{"0"=>{"activity_id"=>"3", "id"=>"1", "_des
troy"=>""}}}
>> ls.update_attributes(params)
=> true
>> Lesson.last
=> #<Lesson id: 1, activity_id: 29, created_at: "2010-08-29 07:57:13", updated_a
t: "2010-08-29 17:04:34", limit: 5>
>> params = {"lessons_attributes"=>{"0"=>{"activity_id"=>"29", "id"=>"1", "_de>
=> {"id"=>"1", "lessons_attributes"=>{"0"=>{"activity_id"=>"29", "id"=>"1", "_de
stroy"=>""}}}
>> ls.update_attributes(params)
=> true
<y_id"=>"3", "id"=>"1", "_destroy"=>""}}, "id"=>"1"}
=> {"id"=>"1", "lessons_attributes"=>{"0"=>{"activity_id"=>"3", "id"=>"1", "_des
troy"=>""}}}
>> Lesson.last
=> #<Lesson id: 1, activity_id: 29, created_at: "2010-08-29 07:57:13", updated_a
t: "2010-08-29 17:04:34", limit: 5>
>> ls.update_attributes(params)
=> true
>> Lesson.last
=> #<Lesson id: 1, activity_id: 3, created_at: "2010-08-29 07:57:13", updated_at
: "2010-08-29 17:13:36", limit: 5>

Which is basically:
the last lesson starts with an activity_id of 29
call update_attributes on the lesson_set to try and set the activity_id to 3
it returns true but when we look at the last lesson it has not changed
then set the activity id to be 29 ie. the same and call update_attributes
activity_id is still 29
but then call again to try and set it to 3
and this time it updates the lesson.???

Is there something wrong with my code this seems really odd behaviour to me and I cant find anything on the net similar.

think I am heading for a head slap moment!

  • 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-18T05:19:14+00:00Added an answer on May 18, 2026 at 5:19 am

    With the models above
    Lesson has_and_belongs_to_many Lesson_sets
    Attendee belongs_to Lesson_set

    trying to put the validation that the should not be more attendees than the limit meant that the association between the lessons and the lesson_sets was being reset during the validation before the new association was saved.

    Moving the validation logic from the lesson_set to the Attendee enabled the validation to work.

    So if you are having something similar try moving your validation logic from the joining model to one of the models that it joins

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

Sidebar

Related Questions

Having trouble getting a simple validation to work in Rails 3, even though the
I'm having trouble getting a rails app on Dreamhost's Passenger to see compiled libraries
I'm looking to try out JRuby and JRuby on Rails. I'm having trouble finding
I'm having significant trouble installing Ruby on Rails. I installed Ruby with apt-get When
I'm new to rails and having trouble figuring out how to do this RESTfully.
I'm having trouble getting a working rails console, and I'm not sure why. I've
I'm having trouble sending mail using SMTP from a Rails app. I have a
I'm having trouble getting rubygems to work in my Rails app. Specifically, I'm trying
I am having trouble understanding ORM in Ruby on Rails. From what I understand
I am having trouble with paths in ruby on rails My Routes: map.resources :companies

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.