sql - ActiveRecord::Relation join, how to add a column of a join table to the query result with a new name? -


To set the step, I am using Rail 3 and I have these tables and relation:

  The user has_many lists list has_many work assignments have_many stints  

I would like to create a query which allows me to select all the current user stints, And list.id is available as a result on each term in the result I need to rename list.id in list_id Awareness will be because otherwise it overwrites the attribute ID term results. I felt that "as" will work, but it is not.

This gives me the last action of the user 1:

  Stint.joins (: task => [[:: list = & gt ;: user]] ). Where (: lists => gt; {user_id = & gt; 1}). Final = & gt; # & Lt; Stutt ID: 753, Task_id: 245 & gt;  

However, what I would like to do is:

  = & gt; # & Lt; Stant ID: 753, Task_id: 245, List_ID: 2 & gt;  

So I thought this would work:

  Stint.joins (: task => [[:: list = & gt ;: user} ]). Choose (where: = lists = & gt; {user_id = & gt; 1}). ('Stints. *, List.id as list_id'). Final = & gt; # & Lt; Stutt ID: 753, Task_id: 245 & gt;  

As you can see, there is no difference but if I do not use "like", then I get:

  Stint .joins (: task = & gt; [{: list = & gt ;: user}]). Select (where:: list = & gt; {user_id = & gt; 1}). ('Stints. *, Lists.id'). Final = & gt; # & Lt; Stutt ID: 2, Task_ID: 245 & gt;  

The list.ID is used, but because the attribute is set to "id", it hides stint.id.

Any help would be appreciated.

Edit - SQL is OK. Returns the query, correct columns, and values ​​with list_ id.
The problem is that it does not make any attributes on the model.

Duh - The solution was there with all the attributes it was not just being displayed in the result. How stupid I think ..

  s = Stint.joins (: task = & gt; [{list: & gt;: user}]). Choose (where: = lists = & gt; {: User_id = & gt; 1}). ('Stints. *, As list_id to Lists.id'). Last = & gt; # & Lt; Stutt ID: 753, Task_id: 245 & gt; S.attributes => {"Id" = & gt; 753, "list_id" => "2", "task_id" => 245} s.list_id = & gt; "2"  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -