Sequel accessor methods

the FAQ link is broken so forgive if i screw up some forum rule

I am trying to add to a SEQUEL database like this:

  def new_house( self_id )
    if DataBase[:house].where( :id =>  self_id ).exists == false then
      DataBase[:house].insert(:id => self_id)
    end
  end

I am trying to check to see if there is a ‘house’ with that id and if
there is not one, make it. It does not work. Any suggestions?

Jake Brightmatter wrote:

the FAQ link is broken so forgive if i screw up some forum rule

I am trying to add to a SEQUEL database like this:

  def new_house( self_id )
>     if DataBase[:house].where( :id =>  self_id ).exists == false then
>       DataBase[:house].insert(:id => self_id)
>     end
>   end

I am trying to check to see if there is a ‘house’ with that id and if
there is not one, make it. It does not work. Any suggestions?

Sequel doesn’t have an FAQ, but the documentation website is up
(http://sequel.rubyforge.org/documentation.html).

Dataset#exists doesn’t do what you think it does. Try:

def new_house( self_id )
DataBase[:house].insert(:id => self_id) unless DataBase[:house][:id
=> self_id]
end

Jeremy

def new_house( self_id )
DataBase[:house].insert(:id => self_id) unless DataBase[:house][:id
=> self_id]
end

I don’t suppose you could explain that syntax could you… what exactly
are you doing?

Jake Brightmatter wrote:

def new_house( self_id )
DataBase[:house].insert(:id => self_id) unless DataBase[:house][:id
=> self_id]
end

I don’t suppose you could explain that syntax could you… what exactly
are you doing?

Sure thing:

DataBase[:house][:id => self_id]

This will return the first row in the database where the id column
matches self_id (as a hash), or nil if no item matches. Since you only
want to insert the row if there isn’t an existing row with the id, if it
returns anything except nil, you don’t call insert.

DataBase[:house].insert(:id => self_id)

This inserts the row with the id column set to self_id.

Jeremy

Let’s assume for a second that I understood 50% of what you said. Based
on that half understood concept, the following code would look at house
locations in the hash and if the location was NIL, it would break out of
the loop?

def find_unused_house_id()
i = 0
b = true
while(b)
@open_lot = DataBase[:house].filter( :id => i ).first
b = false unless DataBase[:house][:id => i]
i += 1
end
end

If this is too far off the mark, and you happen to know Java, C#,
JScript, JavaScript, C or C++, could you explain it in those other
language terms because for some reason, the Ruby syntax is an enigma to
me.

I have no idea what we’re working with, but the Rails framework (I think
activerecord in particular) could help you here, because it implements
things like:

DataBase.find_or_create_by_id :id

Which does exactly what you think it does.

Morning Jake,

On Tue, Sep 29, 2009 at 9:42 AM, Jake Brightmatter
[email protected]wrote:

 b = false unless DataBase[:house][:id => i]
 i += 1

end
end

The following might actually be a little easier to understand - typos
are
not guaranteed to exist or not exist :slight_smile:

def find_unused_house_id(start_point = 0)
while (DataBase[:house][:id => start_point])
start_point += 1
end
start_point
end

This simply takes a starting point and will continue to loop through
until
you get a nil back from the database and then returns the id that got
the
nil back. I added the ability to pass in a starting point if you wanted
to
search from something other then 0 but if you just called the function
without a value then it would start at 0 by default.

It’s slightly different then you posted but I think if you are having
issues
with syntax you should keep things nice and simple and this does that by
merely returning the id and you can do as you please with the value once
you’ve got it.

The while line could read “while (DataBase[:house][:id => start_point]
!=
nil)” but the != is not necessary in this case.

Also, the reason for the start_point line at the bottom is that ruby
will
“return” the last value from the function and therefore just dropping
the
variable you want in as the last line works. You could use “return
start_point” if you wanted but the standard is just to loose the return.

Hope this helps some.

John

def find_unused_house_id(start_point = 0)
while (DataBase[:house][:id => start_point])
start_point += 1
end
start_point
end

This is what I had written when you came alone.

def find_unused_house_id()
parking_spot_number = 0
while(DataBase[:house][:id => parking_spot_number])
parking_spot_number += 1
end
return parking_spot_number
end

eerie right?! You were right on the money of what I wanted. I had
assumed that a ‘nil’ return would be taken as false. By your code
example, i was correct to assume that. I also was explicitly returning
the value, why didn’t you? Is that a ruby thing?

My only issue is that this does not work and I don’t know why. I have
essentially what you have right? It should be noted that I am calling
this function in an odd way. I am using JavaScript from a *.rhtml file
using a direct ‘request’ via this code:

var myHouseRequest = new Request({
url: “/find_unused_house_id/”
}).send();

now I explicitly returned the count from ruby but I have no access to
the variable ‘parking_spot_number’ from the JavaScript. At least I
don’t think I do.

So although I think the ruby code should work, I have no proof that it
is working.

Jake,

On Tue, Sep 29, 2009 at 11:12 AM, Jake Brightmatter
[email protected]wrote:

parking_spot_number = 0

Yes, ruby will implicitly return the last value in a function so the
“start_point” line from my code acts exactly like your return
parking_spot_number line.

My only issue is that this does not work and I don’t know why. I have
essentially what you have right? It should be noted that I am calling
this function in an odd way. I am using JavaScript from a *.rhtml file
using a direct ‘request’ via this code:

var myHouseRequest = new Request({
url: “/find_unused_house_id/”
}).send();

now I explicitly returned the count from ruby but I have no access to
the variable ‘parking_spot_number’ from the JavaScript. At least I
don’t think I do.

So although I think the ruby code should work, I have no proof that it
is working.

You should probably just mock up something that can call into the
function
directly without any javascript or any other layers and ensure first
that
you are getting back the appropriate value from your function. Then work
on
getting the call to work from javascript.

John

You -could- make your life a ton easier and set a class variable with
the total count of created objects. Then you just refer to that variable
and increment by one - bingo, new id. That’s neither here nor there,
though.

“return parking_spot_number” and “start_point” do the same thing in
Ruby.

I don’t understand how you’re calling that function.
Replace it with a dummy function like:

def dummy
return “It’s alive!”
end

And check to see if Javascript properly gets “It’s alive!” or not.

Jake Brightmatter wrote:

def find_unused_house_id(start_point = 0)
while (DataBase[:house][:id => start_point])
start_point += 1
end
start_point
end

This is what I had written when you came alone.

def find_unused_house_id()
parking_spot_number = 0
while(DataBase[:house][:id => parking_spot_number])
parking_spot_number += 1
end
return parking_spot_number
end

eerie right?! You were right on the money of what I wanted. I had
assumed that a ‘nil’ return would be taken as false. By your code
example, i was correct to assume that. I also was explicitly returning
the value, why didn’t you? Is that a ruby thing?

My only issue is that this does not work and I don’t know why. I have
essentially what you have right? It should be noted that I am calling
this function in an odd way. I am using JavaScript from a *.rhtml file
using a direct ‘request’ via this code:

var myHouseRequest = new Request({
url: “/find_unused_house_id/”
}).send();

now I explicitly returned the count from ruby but I have no access to
the variable ‘parking_spot_number’ from the JavaScript. At least I
don’t think I do.

So although I think the ruby code should work, I have no proof that it
is working.

Yes, ruby will implicitly return the last value in a function so the
“start_point” line from my code acts exactly like your return
parking_spot_number line.

for the record, I did see that you had answered that already but I had
already hit the ‘Submit’ button. At that point, all I could do is be
embarrassed.

I will call this one solved!

Thanks to you both (Jeremy and John) for the sharing of your ruby
wisdom. I hope you both have a great day.

Jake

Thanks to you too Aldric, you surreptitiously slipped in an answer under
my nose. :slight_smile: