gematzab
gematzab

Reputation: 181

Persisted Records in Rails

I have to make a functionality for an "Item" Model. The initial requreiment requirement is : - The Item should be persisted and reference the user that submitted them. What exactly means that it should be persisted? Do I have to use the persisted?(Returns true if the record is persisted, i.e. it’s not a new record and it was not destroyed, otherwise returns false.) function ?

Maybe this is too obvious but I have to be sure.

Upvotes: 0

Views: 487

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

I wish I had "Ruby/Rails" classes when I was at school..

Yes. You have to define that Item belongs_to User, so that each item will have user_id (you'll gave to add this column to users table).

Persisted means saved. You can check it as you've mentioned.

You can also go with method in Item class:

def persisted
  map{ |i| i if i.persisted? }
end

or with scope:

scope :persisted, -> { where.not(id: nil) }

Upvotes: 1

Related Questions