disclaimer and I'm ruby/rails n00b.
I have a simple ActiveRecord class:
class LandingPage < ActiveRecord::Base
attr_accessible :name, :parent_id, :published
end
Now, I haven't worked out the pattern yet, but sometime object.name return the right value, other time object.name or, inside the class, self.name return a value like this:
<LandingPage:0x007fd05c605980>
I'm guessing it's because "name" is like a reserved property? But sometimes it works. Can I use "name" as a property or not?
Name is not a reserved property in Ruby and not in Ruby on Rails. I think you're printing out the return value of the to_s
method.
If you're using the poor man's debugger ( Kernel#puts
or Kernel#p
) then it will use the to_s
method.
Try:
puts landing_page.name
instead of puts landing_page
. Or if you're using it in a view, it's the same:
<%= @landing_page.name %>
instead of <%= @landing_page %>
.
if you're inside a class method
class Foo
def self.foo
puts self.name
end
end
Foo.foo
will return the class name which is Foo. however, given the following
class Foo
attr_accessor :name
def self.foo
puts self.name
end
def method1
puts self.name
end
end
Foo.new(name: 'my name').method1
will return my name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.