2

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.