On Thursday, June 20, 2019 at 2:05:48 PM UTC-7, Grant Schoep wrote:
>
> I have this module I am using to do updates on a row in a table that is 
> getting very redundant. I was trying to figure out if I can reduce this by 
> using some type of variable in place of the column name. 
>
> So for example my module has about 10 methods like this (simplified, are 
> some other things going on, but reducing for this question.
>
> def self.set_thing_x(id, value, notes)
>   db[:my_job]
>     .where(job_id: id)
>     .update(stage: notes, thing_x: value)
> end
>
> def self.set_thing_y(id, value, notes)
>   db[:my_job]
>     .where(job_id: id)
>     .update(stage: notes, thing_y: value)
> end
>
> The only thing that is really different, is the name of the column. I know 
> the name of the column, hence I picked the right method name. 
> This must be simple, I am just missing it.
>
> FYI db in the above code is basically a Sequel::Postgres::Database object
>

You should make the column an argument to the method:

def self.set_column(id, column, value, notes)
  db[:my_job]
    .where(job_id: id)
    .update(stage: notes, column => value)
end

def self.set_thing_x(id, value, notes)
  set_column(id, :thing_x, value, notes)
end

def self.set_thing_y(id, value, notes)
  set_column(id, :thing_y, value, notes)
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/9fafe044-bd05-4e11-b085-7a576dbce574%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to