Reputation: 1052
Can somebody help me to insert in my instance an attribute with always the same value?
Actual:
#<Test surname: "example", givename: "test">
Target:
#<Test surname: "example", givename: "test", newvalue: "text">
How do I have to change this code?
@users.each do |user|
user
end
Or can I implement this with something like :attr_accessor
?
Upvotes: 2
Views: 230
Reputation: 4113
If you really need the same value for all instances of your class you can do following:
class Some
def initialize
@value = "Some value"
end
def newvalue
@value
end
end
this will give newvalue
with "Some value" text for all instances of class Some or whatever you call it. Hope it will help.
Upvotes: 1
Reputation: 8954
You need to add a new column to your Model. For that you can use a migration. First create a migration like this:
rails g migration addNewValueToTests
Then you will find the migration file in db/migrations/
there you add a add_column
to the self.up
method like this:
def self.up
add_column :table_name, :column_name, :datatype #datatype like integer, sting, text,....
end
then you migrate the database with the rake db:migrate
command and the column + setter/getter methods will be added to the model!
// If you just want an alias you can use alias_attribute
in the model like this:
alias_attribute :new_name, :current_name
Upvotes: 0