Reputation: 41
I have a user table that has a bunch of attributes that I can call with a method (This was generated by devise):
user.name
However I made another table, as the user has_one server. These are linked by rails' foreign key magic. I want to be able to call methods like I do with the user table, by using methods, but it always throws this error when trying to call user.server.created_at
NoMethodError: undefined method `created_at' for #<ActiveRecord::Relation:0x000001036e5a38>
I then tried to do this:
user.server[:created_at]
And that threw this error:
Typerror: can't convert Symbol into Integer
I heard attr_accessor might be appropriate to create methods, but I'm not really sure where to start using it with Rails, and if that would even be a good idea.
EDIT:
I'm trying to end up with something like this:
time_left = (Time.now.to_i - current_server[:created_at].to_i)
Except with correct syntax. Created_at does exist and is being written with the correct time.
User Model:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :server
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
Server Model:
class Server < ActiveRecord::Base
belongs_to :user
end
Thanks!
Upvotes: 0
Views: 113
Reputation: 26979
Does the created_at column actually exist in the table named servers?
The second syntax is just wrong. I don't know why it gives that error, but I dont expect it to work anyway.
Posting more full code might help debug.
Upvotes: 1