Hi,
I'm trying to get records from database, which are associated with another record based on conditions. This record can have either a "has_many" or "has_one" association to the records I'd like to fetch.
If it's "has_many", I can simply run
known_record.records_to_fetch.find(:all, :conditions => {...})
which will either give me the records which match the conditions or []. My problem is to get a record with a has_one association only in the case the conditions match the record - otherwise nil - , so something like
known_record.get_has_one_record_if_conditions_match(:field => "..." ...)
I could loop over all my conditions and test if they match the has_one record, but I thought, there might be a more elegant way to do it in rails. Does anyone have an idea?
Thanks in advance!
Edit:
My structure is as follows: In my application, you can assign skins (for a booking frontend) to almost everything (the current subdomain, a ticket, a user, etc). When someone visits the website, I have to find out which skin to use, so I start with e.g. an event, if I don't find anything, I go up to user, subdomain, etc.
Now, an event has just one skin (has_one), a user might have many skins (has_many). I don't want to do all this searching manually, so I wrote a function which bubbles up automatically and checks for both, model.skins() and model.skin. This is why I wanted to get a condition validation for model.skin().
I think, I could do it using Skin.find and merge e.g. the event_id into my conditions, but this seems ugly again.