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.

You'll probably need to override the add_association method and do the 
find_or_create logic there (calling super with the appropriate object).

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.

Reply via email to