On Sun, Feb 27, 2022 at 2:03 PM Bromley Jones <bromley.jo...@gmail.com>
wrote:

> Hello
>
> Very new to Sequel and Ruby, so bear with
>
> The idea is to return model instances of the table_1 model where the id's
> of table_1 do not exist in table_2 where another_id has a value. This is so
> I can use those instances and the data contained within elsewhere
> def self.my_method(another_id:)
>  result = DB["SELECT t1.* FROM table_1 t1 WHERE t1.id NOT IN (SELECT t2.id
> FROM table_2 t2 WHERE t2.another_id = #{another_id})"]
>
>  result_array = []
>  result.each do |row|
>      result_array << self.find(id: row[:id])
>   end
>
>  result_array
> end
>
> I have models for both table_1 and table_2
>
> This works but it feels very clunky and probably very inefficient as for
> every id that is returned I am rebuilding an instance of each table_1 model
> by looking at the database again.
>
> How would this be done in a typically Sequel way using the methods made
> available on the Model/Dataset. From my viewpoint I am struggling with how
> I pull together the various pieces of syntax to do this
>
Looking at your query, the column qualifications are unnecessary since you
are never doing a join.  You could probably implement the method as follows
(assuming the method is defined in the model for table1):

def self.my_method(another_id:)
  exclude(id: DB[:table2].where(another_id: another_id).select(:id)).all
end

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSe4ajXPCjED84n-fuTPEzX35-uHWrinmn4TqT1TxR%3DrPQ%40mail.gmail.com.

Reply via email to