[Nitro] Accessing a database column with two different names

Jonathan Buch john at oxyliquit.de
Wed Oct 25 17:48:12 EDT 2006


Hi,

> class StorageFile
>
> 	property :filename, String
> 	property :realname, String
> 	property :content_type, String
> 	has_one :owner, StorageUser
> 	has_many :viewers, StorageUser
>
> 	validate_unique :filename
>
> end
>
> class StorageUser
>
> 	property :username, String
> 	property :password, String
> 	property :realname, String
> 	property :email, String
       has_many :owned_files, StorageFile
>
> end

Look at the change here, I refer to that one later.

> When I tried finding all files belonging to I got an error:
>
> @myfiles = StorageFile.find do |file|
>      file.owner == some_storageuser.oid
> end
>
> as the column 'owner' doesn't exist.
>
> I then checked SQLite for the table definition:
>
> sqlite> .schema ogstoragefile
> CREATE TABLE ogstoragefile (filename text, owner_oid integer, oid
> integer PRIMARY KEY, content_type text, realname text);
>
> which claims I should be accessing the owner with the 'owner_oid' name.
> I find this quite unintuitive. If I use 'owner' when I create an object
> I expect to be able to reuse that name when extracting information from
> the very same object. Is this a bug or by design?

This is by design.

When you extend the model like I did above, you can do this:

@myfiles = some_storeageuser.owned_files

Looks better (, avoids calling Ez) and is much more convenient.

But using your query example:  You're comparing apples with oranges
(oid (an integer, 'oid') with a model (an object, 'owner')).  So using
`file.owner_oid == some_storageuser.oid` makes imo more sense.
Or even `file.owner == some_storeageuser`.  That said, I know nothing
about Ez and how it creates SQL statements, I tend to avoid it.

Hope that helps,

Jonathan

-- 
Feel the love
http://pinkjuice.com/pics/ruby.png


More information about the Nitro-general mailing list