문제

I'm just starting to use Sequel in Ruby, and like it alot.

I want to pass a variable to the "from" method. So instead of calling a method like so:

DB.from(:items)

I'd like to call the method with a variable. For example:

# both of the following approaches fail
tableName = "items"
DB.from(tableName)
DB.from(:tableName)

But it fails with a sql error about a value that's not in my variable. I don't think this is a Sequel issue... I think it's a "I'm new to Ruby" issue.

How can I pass a variable to the from method above?

도움이 되었습니까?

해결책

Do as below using String#to_sym method :

DB.from(tableName.to_sym)

Looking at the documentation of Sequel::Database#from, it seems it accepts all arguments as symbols. Thus you need to convert the string object pointed by the local variable tableName, to a symbol object.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top