I have these sql code in postgresql "SELECT * from convenios where id NOT IN (SELECT convenio_id from solicituds where usuario_id=?" but don''t find a way of used it in rails except find_by_sql. There is a another way?? class Usuario < ActiveRecord::Base belongs_to :persona has_many :solicituds, :dependent => :destroy has_many :convenios, :through => :solicituds has_many :nivelesacceso has_one :nomina end class Convenio < ActiveRecord::Base has_many :solicituds, :dependent => :destroy has_many :personas, :through => :solicituds validates_presence_of :nombre, :serie, :descripcion validates_uniqueness_of :serie validates_format_of :serie, :with => /cci|cue/ validates_length_of :serie, :is => 8 end class Solicitud < ActiveRecord::Base belongs_to :convenio belongs_to :usuario end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 11 November 2010 21:53, Ich <aconsuegra2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have these sql code in postgresql > "SELECT * from convenios where id NOT IN > (SELECT convenio_id from solicituds where usuario_id=?"That does not look like valid sql to me. Can you explain in words what you are trying to achieve? I find this is often the best way of working out how to code something. Colin> > but don''t find a way of used it in rails > except > find_by_sql. > > There is a another way?? > > class Usuario < ActiveRecord::Base > belongs_to :persona > has_many :solicituds, :dependent => :destroy > has_many :convenios, :through => :solicituds > has_many :nivelesacceso > has_one :nomina > end > > class Convenio < ActiveRecord::Base > has_many :solicituds, :dependent => :destroy > has_many :personas, :through => :solicituds > validates_presence_of :nombre, :serie, :descripcion > validates_uniqueness_of :serie > validates_format_of :serie, :with => /cci|cue/ > validates_length_of :serie, :is => 8 > end > > class Solicitud < ActiveRecord::Base > belongs_to :convenio > belongs_to :usuario > end > > > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I want all the "convenios" that don''t have been requested by a particular user. On 12 nov, 08:49, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 11 November 2010 21:53, Ich <aconsuegra2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have these sql code in postgresql > > "SELECT * from convenios where id NOT IN > > (SELECT convenio_id from solicituds where usuario_id=?" > > That does not look like valid sql to me. Can you explain in words > what you are trying to achieve? I find this is often the best way of > working out how to code something. > > Colin > > > > > but don''t find a way of used it in rails > > except > > find_by_sql. > > > There is a another way?? > > > class Usuario < ActiveRecord::Base > > belongs_to :persona > > has_many :solicituds, :dependent => :destroy > > has_many :convenios, :through => :solicituds > > has_many :nivelesacceso > > has_one :nomina > > end > > > class Convenio < ActiveRecord::Base > > has_many :solicituds, :dependent => :destroy > > has_many :personas, :through => :solicituds > > validates_presence_of :nombre, :serie, :descripcion > > validates_uniqueness_of :serie > > validates_format_of :serie, :with => /cci|cue/ > > validates_length_of :serie, :is => 8 > > end > > > class Solicitud < ActiveRecord::Base > > belongs_to :convenio > > belongs_to :usuario > > end > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 12, 6:59 am, Ich <aconsuegra2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want all the "convenios" that don''t have been requested by a > particular user.This is not tested, but I think something like this should get you what you want. May need to tweak it a little bit. Convenio.joins(:solicituds).where("solicituds.usuario_id <> ?", 1234) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Don''t work for me. A last I use arreglo= Array.new arreglo=solicituds.all(:select => "convenio_id").map(&:convenio_id) arreglo<<-1 Convenio.where(["id NOT IN (?)",arreglo]) I''m not happy but works. On 12 nov, 09:27, Tim Shaffer <timshaf...-BUHhN+a2lJ4@public.gmane.org> wrote:> On Nov 12, 6:59 am, Ich <aconsuegra2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I want all the "convenios" that don''t have been requested by a > > particular user. > > This is not tested, but I think something like this should get you > what you want. May need to tweak it a little bit. > > Convenio.joins(:solicituds).where("solicituds.usuario_id <> ?", 1234)-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Please quote when replying. Ich wrote in post #961576:> Don''t work for me.Are you using Rails 2 or 3? The syntax that you''re responding to was for Rails 3 (or Rails 2 + Arel).> A last I use > > arreglo= Array.new > arreglo=solicituds.all(:select => > "convenio_id").map(&:convenio_id) > arreglo<<-1 > Convenio.where(["id NOT IN (?)",arreglo]) > I''m not happy but works.That''s not even good SQL. I think you want something like this SQL: SELECT * FROM convenios c LEFT JOIN solicituds s ON (s.convenio_id = c.id AND s.usuario_id = :user_id) WHERE s.id IS NULL That gets everything in one query, and will be pretty easy to translate into AR find arguments, so you won''t need find_by_sql here. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.