0

I have a similar question to this guy Rails: How to limit number of items in has_many association (from Parent)

The key is I'd like to do this on the Array.push rather than on the :before_save attribute of the has_many association. In Java, I would probably make .windows private and create my own accessor. Not sure if I can do that with ActiveRecord methods that are available as the result of an association.

Any suggestions?

The spec I'm trying to get to pass is:

it "should not accept anymore windows" do
    channel = Channel.new #with default 3 windows
    channel.windows.length.should == 3
    channel.windows.push Window.new
    channel.windows.length.should == 3
end
2
  • Not an answer to your question, but is it your intent to make this fail silently (i.e. not raise any kind of error when you do it)? Feb 9, 2012 at 14:17
  • Marc: Not really. Though that probably would have been fine. This was just a case where I needed to be sure that no more than 3 windows would be created. So failing silently and just not adding the window would have worked. It probably wouldn't have been acceptable for consumers of my class though.
    – Greg
    Mar 9, 2012 at 20:03

3 Answers 3

2

You can use a before_add callback (scroll down to heading "Association callbacks") on your association to enforce the behavior

Should any of the before_add callbacks throw an exception, the object does not get added to the collection. Same with the before_remove callbacks; if an exception is thrown the object doesn’t get removed.

3
  • That was suggested in the other thread. The problem is, that it allows it to be added to the object, but fails when the .save method is called. Also, it doesn't prevent the child from getting created, which means I'd have to implement a rescue block to delete the child. It all seems a bit kludgy for RoR
    – Greg
    Feb 10, 2012 at 2:31
  • Spoke a bit too soon. The second bit about it creating a child isn't true. But still, I could add as many windows to a channel as I want and the exception isn't raised until I try to save the object.
    – Greg
    Feb 10, 2012 at 2:48
  • Ransom.. While this isn't what I wanted, it ultimately ended up being the best solution given everything else that's going on with ActiveRecord. Even if I could override 'push' and I found a way to do that, I'd also have to override << create and build.
    – Greg
    Feb 11, 2012 at 4:35
1

A little bit better than a callback, but not as clean as what you're trying to achieve, would be to do something like channel.windows << elem unless channel.windows.length > N.

1
  • I like the syntax, but that would have to be in the controller, not the model, where IMHO it belongs.
    – Greg
    Feb 10, 2012 at 2:29
0

Why don't you control window insertion by making it through a method,

#chanel.rb
class Chanel
  def add_window(window)
    windows.push window if windows.length < 3
  end
end
1
  • In Java, where I've done more work, I would make Channel.windows private and create a setter like you suggested. But I don't want my code to be such that someone could come along after the fact and just push something onto the array without realizing there was a "special" method to restrict that.
    – Greg
    Feb 10, 2012 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.