views:

311

answers:

2

Hello there, Well, I don't know if I'm completely wrong, but I really can't find a very practical and straight forward way to do something like this:

class User < ActiveRecord::Base

has_many :creations

end

but the thing is, I just want the user to have many creations if the user.developer == true where user.developer is just a boolean field inside the Users table.

So any ideas on how exactly could I do it directly from the model?

Resuming, when the user is not a developer if you try to get User.first.creations, User.first.creations.new ... create...destroy, etc you get a NoMethodError but if it is a developer you can build a new creation.

The only way I managed to do it is extending the model and from the extension check if the proxy_owner.developer == true but by doing this I had to rewrite all the actions such new, create, update, etc...

Any help would be much appreciated Thanks a lot

A: 

Including this may work. If not you may have to resort to alias_method_chain, but I hear that has links to serious organised crime, so watch yourself.

module CreationsJustForDevelopers
  def creations(*args)
    if developer?
      super
    else
      raise NoMethodError, "Only developers get creations."
    end
  end
end

Not sure what you are referring to with all that talk of overriding new, create, update etc… but the only other method I can think of to remove is creation_ids, but who cares about that?

cwninja
Thanks for this cwninja it really opened my mind about implement on a different way. but unfortunately it seems that the mixin doesn't override the **has_many :creations** method, so it doens't take effect.probably I'm doing something wrong.I tried to declare the creations function after the **has_many :creations** and it worked on find when its developer and when its not but when developer? I'm getting the following: **NoMethodError: super: no superclass method `creations'**I'll try to keep trying get some result from this,Thanks for you help again :)
ludicco
Jared's answer above is still neater, unless for some reason you are forced to do it this way.The no superclass method may be due to you including the module before the association has been set up. Try moving it to after the `has_many :creations`.
cwninja
Yes, I really notice that cwninja. Probably I'll leave like this for the moment till get more experience in tweak rails internals.But Thanks a lot for your help
ludicco
+2  A: 

How about subclassing User and only specifying the has_many on the developer subclass? Developer would then pick up any logic from User and Users wouldn't have any creations.

class User < ActiveRecord::Base
end

class Developer < User
  has_many :creations
end
Jared
This really is the way to do it. Classic example of a behaviour that should be modeled in inheritance. You will need to have a quick read on rails STI to see how to set it up, its really very easy. Then you get to do things like Developer.find_blah as well.
dalyons
Thanks for that mates, actually I did it before come with the 'excellent' idea to use less models on my app. I was thinking that isn't necessary to have a new model for that since its a very simple thing, so I started to change my my. But as I'm seeing probably I'll have to go back to this idea. But one thing for sure that is really intrigant is the fact that there's no 'easy way' to say: "ok, I just want to this model to have_many :blas if this model have this atrribute", besides create a new model.
ludicco
I tried this ( http://pastebin.com/f3eb450a3 ) And it worked on the first call but after it generates the has_many :apps for the first time it attaches it to the class so no way you can override it anymore.But, thanks for your help, it's really appreciated
ludicco
FYI, you can define more than one model inside of a file. Take the code I gave above and throw it all into User.rb in your 'app/views/models' directory.
Jared
Thank Jared, sometimes you get stuck on just one thought but I really noticed this way is much better and even organized.I can have much more control with a model specific for that.Thank you your help Jared
ludicco
Happy to be a help ludicco. You could mark my answer correct if that's the one you went with. :)
Jared