I'm working on a RubyonRails/ActiveAdmin application. My RoR version is 4.2.5 and AA version is 1.0.0. I have a model Message
as follows.
我正在开发RubyonRails/ActiveAdmin应用程序。我的RoR版本是4.2.5,AA版本是1.0.0。我有如下的模型消息。
class Message < ActiveRecord::Base
belongs_to :user
validates :user, :content, presence: true
def palindrome
# return true/false
end
end
As you see, I want to have a read-only attribute palindrome
which only depends on the content
of message. I want this attribute to be treated exactly like a normal attribute. By normal, I mean when I retrieve messages via rails console
or request json
format of messages, I want to see a palindrome
attribute in the list. I would also like to have a filter for message by this attribute.
如您所见,我想要一个只读属性palindrome,它只依赖于消息的内容。我希望这个属性与普通属性完全相同。通常,当我通过rails控制台检索消息或请求消息的json格式时,我希望在列表中看到一个palindrome属性。我还希望使用此属性对消息进行筛选。
I'm not sure how could I achieve this.
我不知道我怎么能做到这一点。
2 个解决方案
#1
11
In your model, you can write attribute accessors (reader/writer) for your virtual attribute palindrome
attribute this way:
在您的模型中,可以通过以下方式为您的虚拟属性palindrome属性编写属性访问器(reader/writer):
# attr_reader
def palindrome
self[:palindrome]
end
# attr_writer
def palindrome=(val)
self[:palindrome] = val
end
# virtual attribute
def palindrome
#return true/false
end
And, as you are using Rails 4, you have to whitelist palindrome
attribute like any other model attribute in your strong param definition inside your controller in order to able to mass assign the value of palindrome
. Something like this:
而且,在使用Rails 4时,您必须像控制器内部的强参数定义中的任何其他模型属性一样,对palindrome属性进行whitelist,以便能够批量分配palindrome的值。是这样的:
# your_controller.rb
private
def your_model_params
params.require(:message).permit(:palindrome)
end
Take a look at this RailsCast on Virtual Attributes. Although, it's a bit old, but would be useful for concepts.
看看这个关于虚拟属性的RailsCast。虽然有点旧,但是对概念是有用的。
Note:
注意:
A virtual attribute will not show up in the param list automatically. But, you should be able to access it via Rails console like this: Message.new.palindrome
. Also, you can expose this virtual attribute in your JSON API, for example if you are using Active Model Serializer, you can have: attribute palindrome
in your MessageSerializer
and then palindrome
will be exposed to the JSON API.
虚拟属性不会自动显示在param列表中。但是,您应该能够通过Rails控制台像这样访问它:Message.new.palindrome。同样,您可以在JSON API中公开这个虚拟属性,例如,如果您正在使用活动模型序列化器,您可以在MessageSerializer中拥有:attribute palindrome,然后palindrome将公开给JSON API。
#2
20
Rails actually let's you create virtual attributes this way, which keeps you from having to manually create getter and setter methods:
Rails实际上让我们以这种方式创建虚拟属性,这使您不必手动创建getter和setter方法:
attr_reader :palindrome #getter
attr_writer :palindrome #setter
attr_accessor :palindrome #both
You can also pass multiple arguments too:
你也可以传递多个参数:
attr_accessor :palindrome, :foo, :bar
#1
11
In your model, you can write attribute accessors (reader/writer) for your virtual attribute palindrome
attribute this way:
在您的模型中,可以通过以下方式为您的虚拟属性palindrome属性编写属性访问器(reader/writer):
# attr_reader
def palindrome
self[:palindrome]
end
# attr_writer
def palindrome=(val)
self[:palindrome] = val
end
# virtual attribute
def palindrome
#return true/false
end
And, as you are using Rails 4, you have to whitelist palindrome
attribute like any other model attribute in your strong param definition inside your controller in order to able to mass assign the value of palindrome
. Something like this:
而且,在使用Rails 4时,您必须像控制器内部的强参数定义中的任何其他模型属性一样,对palindrome属性进行whitelist,以便能够批量分配palindrome的值。是这样的:
# your_controller.rb
private
def your_model_params
params.require(:message).permit(:palindrome)
end
Take a look at this RailsCast on Virtual Attributes. Although, it's a bit old, but would be useful for concepts.
看看这个关于虚拟属性的RailsCast。虽然有点旧,但是对概念是有用的。
Note:
注意:
A virtual attribute will not show up in the param list automatically. But, you should be able to access it via Rails console like this: Message.new.palindrome
. Also, you can expose this virtual attribute in your JSON API, for example if you are using Active Model Serializer, you can have: attribute palindrome
in your MessageSerializer
and then palindrome
will be exposed to the JSON API.
虚拟属性不会自动显示在param列表中。但是,您应该能够通过Rails控制台像这样访问它:Message.new.palindrome。同样,您可以在JSON API中公开这个虚拟属性,例如,如果您正在使用活动模型序列化器,您可以在MessageSerializer中拥有:attribute palindrome,然后palindrome将公开给JSON API。
#2
20
Rails actually let's you create virtual attributes this way, which keeps you from having to manually create getter and setter methods:
Rails实际上让我们以这种方式创建虚拟属性,这使您不必手动创建getter和setter方法:
attr_reader :palindrome #getter
attr_writer :palindrome #setter
attr_accessor :palindrome #both
You can also pass multiple arguments too:
你也可以传递多个参数:
attr_accessor :palindrome, :foo, :bar