Including an association if it exists in a rails query -
Update: Something that might not be possible
TLD: How do you make a union Conditional loads (say, association load only for existing users), while the record is included in which the association is not included at all?
Rail 3.1, here's the model with which I'm working.
class user has_many: subscriptions_many: archive ,: through = & gt; : Subscription and Class Collection is has_many: Things and Class Thing is_Im: User_Mest_States, Dependent = & gt; : Is destroyed: belong_to: collection end membership to membership belong_to: user belongs_to: archive end class UserThingState belongs_to: user is_to: thing end There are many things that exist in many collections Subscribes to the collections and thus they subscribe to many things. Users have a state in relation to things, but not necessarily, and still subscribe to those things, even if there is no situation for them. When a user subscribes to a collection and its related things, then a state is not prepared for everything (which can be in hundreds). Instead, states are generated when a user interacts with something first. Now, the problem: I want to select all the users to subscribe, while loading the position of the user in every situation where the state is present.
Conceptually it is not difficult for reference, for SQL, which will get me the required data for it:
Select Things *, User_thing_states. * Things from # next line I join INNER to subscribe to all things as subscribing things. Collection_id = subs.collection_id and subs.user_id =: user_id # The next line pulls in state data for the user, To use anything as YouTube on YouTube, UISIID = YoutubeIssidId and UKUIDIID = User_ID I do not know how to slice it together in a rail. What happens in Las? Thing.includes (: user_thing_states) will load all the states for all users and which looks just like a tool. I want something like this, but I'm not sure how (or if it is possible):
is class THING_I: user_stom_service representative: some_state_property ,: to = & gt; : State,: allow_nil = & gt; True DF state # should have the same user_state_state, if it is included, state mode to use it. Need anything for yourself: Thing.includes (: user_question_states, ** where 'user_question_state.user_id = & gt;: User_id **). By_collections (user.collections) then I can
things = user. Things_subscribe_tags.first_state_property # property loaded state for current user
You do not have to do anything.
is a square user: medium: user_cast_state_maya: things, via = gt; : User_thing_states End # All users loaded through w / state associations Users. All users (user exists for) User / All User Load / Specific Things (if present for user) # Lookup Specific User Search user = Using the User.find_by_login 'bob' User.user_thing_states.all (: Include =>: Things) is included () for it it already loads the related object if they exist. Need to do any filtering or add extra behavior for those users who do not have any related objects.
Comments
Post a Comment