A simple way would be to just call <model>.find_by_sql(<sql query>)

On the other hand, if you create a view in MySQL to handle the query, say the 
view is called "friends", then you should be able to create a model like this:

 app/models/friend.rb

class Friend < ActiveRecord::Base
end

Obviously, depending on the view and tables behind it, you might want to make 
your new model read only. That can apparently be done like this (I haven't 
actually tested this!)


class Post < ActiveRecord::Base

  def readonly?
    return true
  end

end

There are probably lots of other ways of wrapping the result as an object, but 
basically I don't think you can avoid passing the SQL UNION statemenet as is. 
There might be a way of calling two separate SQL statements, store the results 
in separate variables and then adding the two:

@var1 = @friends.where(<select 1>)
@var2 = @friends.where(<select 2>)

@friendsfound = @var1 + @var2

Obviously, the returned column names would have to match, and I'm not at all 
sure this would do what it looks like doing!


-----Oprindelig meddelelse-----
Fra: rubyonrails-talk@googlegroups.com 
[mailto:rubyonrails-talk@googlegroups.com] På vegne af Fresh Mix
Sendt: 22. december 2011 23:53
Til: rubyonrails-talk@googlegroups.com
Emne: [Rails] Re: SV: Simple friendship table

> (SELECT uid2 AS uid, status FROM friendships WHERE uid1 = 1) UNION
> (SELECT uid1,status FROM friendships WHERE uid2 = 1)

How to write same in Rails 3 Active Record Query format?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.



-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to