Thanks Jeremy. On 19 Jun 2012, at 16:37, Jeremy Evans wrote:
> On Monday, June 18, 2012 8:10:53 PM UTC-7, Iain Barnett wrote: > Hi all, > > If I use the `_add_association` method to customise an association, would > anyone know of a way to access the options passed when setting up the > association? For instance, how would I get the :join_table while within the > method, instead of just looking up the declaration in the code? I had a look > through the docs and API reference but didn't find the answer. Or is the > answer just to not bother with the options because I'm defining the method > manually anyway? > > The assumption is that if you are overriding the method, it is specific to > the association, so you would just hard code the parameter values. If you > really don't want to do that, you can access the association reflection via > the Model.association_reflection class method. > > My specific use case right now is to override a many to many association's > add method to find or create the associated object from the passed in term, > and then add the record to the join table. > > I'm not sure what you mean by "passed in term". Usually you would pass in a > model object (new or existing), a hash (in which case a new model object is > created), or a primary key (in which case the existing model object is looked > up). By the time the _add_association method is called, the model object (if > new) has been saved, so I'm not sure how your find or create logic can work > inside the _add_association method. By "passed in term" I meant that I was passing in a string and then using that to find_or_create the object for the next argument, but I realise that can't be done this way now. > > You'll probably need to override the add_association method and do the > find_or_create logic there (calling super with the appropriate object). So I need to use super with the _add_association method too? (I didn't find an example of this in the docs except with before/after hooks, sorry if it's somewhere obvious) > > Thanks, > Jeremy > > -- > You received this message because you are subscribed to the Google Groups > "sequel-talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/sequel-talk/-/MrNhvXQ-BWkJ. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/sequel-talk?hl=en. -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en.
