Reputation: 6300
I probably didn't understand STI or whatever inheritance of rails :)
I need to implement a design where I have a form; on upload I submit, but if the user is not logged in, she needs to login via openid (only access supported). Thus this implies a redirect, and the best thing I could come up with so far is to temporarily save the data, and on successful login actually create the real object. I wanted to have two separate tables for the objects; the Site
object is the one to be created, the Sitetmp
is the temporary store, and it has an additional field called nonce; (on successful login, the nonce will be compared, and if ok, the Sitetmp
instance deleted and a new Site
one created)
The DB:
#schema.rb
create_table "sites", force: true do |t|
t.string "name"
t.date "date"
t.text "description"
t.float "lat"
t.float "lon"
t.date "updated"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "sitetmps", force: true do |t|
t.string "nonce"
t.string "name"
t.date "date"
t.text "description"
end
The Subclass:
#app/models/sitetmp.rb
class Sitetmp < Site
attr_accessor :nonce
end
The Superclass:
#app/models/site.rb
class Site < ActiveRecord::Base
validates :name, uniqueness: true, :presence => true,
:length => { :minimum => 5 }
validates :date, :presence => true
has_many :images, :inverse_of => :site
end
It seemed to be all set, but when I actually want to access the temporary object on successful login, it tells me
SQLite3::SQLException: no such column: sites.nonce: SELECT "sites".* FROM "sites" WHERE "sites"."nonce" = '059253928646750523787961570357' LIMIT 1
The code in the controller causing this is:
if nonce
@tmp = Sitetmp.find_by nonce: nonce
Clearly I am trying to access the Sitetmp
instance by its nonce
attribute, but rails
is resolving to actually access the Site
class - which doesn't have a nonce
attribute.
What am I doing wrong? How do I correctly find the Sitetmp
object by nonce
in order to create a valid Site
object from it?
Upvotes: 1
Views: 49
Reputation: 3516
As I understand it, ActiveRecord::Base
has a table_name
method. If you inherit from ActiveRecord::Base
, the subclass (Site
), through some rails magic, has the table_name
set to the underscored lowercased version of itself.
Then inheriting from the Site
class, Sitetmp
does not get the advantage of having the table_name
set.
You may be able to fix this by setting this:
#app/models/sitetmp.rb
class Sitetmp < Site
# Set custom table name
table_name "sitetmp"
attr_accessor :nonce
end
I don't know if this would have an impact on the table_name
of the Site
class, so you may just have to create a standalone Sitetmp
model to work from.
Alternatively, you could load the values into the session
- see here for more
Upvotes: 1