[raleigh.rb] Association Question - Rails

Alfredo Quiroga-Villamil lawwton at gmail.com
Thu Feb 12 11:42:49 EST 2009


Ok, so I am very close I think to getting this to work. I currently have:

tickets = Ticket.find( :all, :include => [:users, :pvr_status, :priority] )

In there I can have the :conditions clause allowing me to dynamically
filter properties with a limit at the end for pagination. One last
thing to put it all together, how can I also add the roles into the
mix?

Thanks in advance,

Alfredo


On Thu, Feb 12, 2009 at 10:48 AM, Alfredo Quiroga-Villamil
<lawwton at gmail.com> wrote:
> Hello All:
>
> So I have another question regarding associations. This one is very
> closely related to a question I had a few days ago. I'll try to
> describe it here the best I can.
>
> I have the following tables. Please note that I am only mentioning the
> relevant fields.
>
> users
> --------
> id
> firstname
>
> roles
> --------
> id
> name
>
> roles_users
> -----------------
> user_id
> role_id
>
> tickets
> ----------
> id
> ticket_number
> ticket_status_id
>
>
> tickets_users
> --------------------
> ticket_id
> user_id
>
> ticket_statuses
> ----------------------
> id
>
> The issue I currently have that is creating all sorts of problems for
> me is the following. I would like to list all the tickets with their
> associated owners(users). As you can see from the tables above, a
> ticket habtm users. What I end up with now using a sql query is a list
> of tickets but with multiple rows for the same ticket since I could
> have multiple users or owners for that record.
>
> So I end up with: (fake table below, just used as an example)
>
> ---------------------------------------------
> ticket id | description | owner
> ---------------------------------------------
> 1              foo             Alfredo
> 1              foo             RaleighUser
>
> I could try to get a list of tickets, then iterate over those and find
> out the users assigned to that specific ticket, concatenate them,
> etc... and format the data. That's fine, most of the time. However
> when I try to do pagination, dynamically filter by other ticket
> properties such as status, ticket number etc... things get very ugly
> very quickly.
>
> So what I am looking for is the correct approach on how to handle all
> these tables using the associations I've already created and end up
> with a query that allows me to add conditions to it so I can do
> filtering in a nice clean way.
>
> I appreciate any help, guidance regarding this. I apologize for the long email.
>
> Regards,
>
> Alfredo
>


More information about the raleigh-rb-members mailing list