Tag: inheritance

link_to与inheritance的Active Record类发生问题

以下是我设置的类: class Stat < ActiveRecord::Base belongs_to :stats_parent end class TotalStat < Stat belongs_to :stats_parent end #The StatsParent class is just to show how I use the relation. class StatsParent < ActiveRecord::Base has_one :total_stat has_many :stats end 对于统计控制器索引操作: def index @stats = Stat.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @stat } […]

为什么rails不会在单表inheritance中保存类型字段

我有一个模型客户端,具有单表inheritance。 但是当我尝试提交表单时,类型字段不会保存在数据库中。 如何强制它保存类型,然后在index.html.erb上显示帐户类型。 车型/ client.rb class Client < ActiveRecord::Base end class Suscriber < Client end class NonSuscriber < Client end 意见/ _form.html.erb clients_controller.rb def index @clients = Client.where(:type => params[:type]) respond_to do |format| format.html format.json {render json: @clients} end end def new @client = Client.new respond_to do |format| format.html # new.html.erb format.json { render :json […]

Rails模型中的inheritance模型

我正在为我的应用做一个报告系统。 我创建了一个模型ReportKind,但是因为我可以报告很多东西,所以我想制作不同类型的报告类型。 由于它们共享很多行为,我正在尝试使用inheritance。 所以我有主要模型: model ReportKind << ActiveRecord::Base end 并创建了例如: model UserReportKind << ReportKind end 在我的表report_kinds中我是类型列,直到这里它全部工作。 我的问题在于表单/控制器。 当我执行ReportKind.new ,我的表单使用’* report_kind *’前缀构建。 如果获取UserReportKind ,即使通过ReportKind.find ,表单也会构建’user_report_kind’前缀。 这会使控制器中的所有内容变得混乱,因为有时候我会有params [:report_kind],有时候是params [:user_report_kind],等等我所做的每一个其他inheritance。 无论如何强迫它使用’report_kind’前缀? 此外,我不得不强制控制器中的属性“类型”,因为它没有从表单直接获取值,是否有一个很好的方法来做到这一点? 路由是另一个问题,因为它试图基于inheritance的模型名称构建路由。 我通过在指向同一控制器的路由中添加其他模型来克服这个问题。

如何使用Inherited Resources Rails插件基于成功或失败自定义Flash消息?

我在2.3.5 Rails应用程序中使用inheritance的资源插件,并且想知道如何根据我的创建和更新操作中的成功或失败来更改flash [:notice](或任何其他flash)。 因此,如下所示,如果成功,我如何添加flash [:notice] =“All good”…如果失败,闪存[:notice] =“All bad”? 谢谢 class ArticleController [:create, :update] def create #init new game @article = Article.new set_article_attributes_from_app @article.is_published = params[:article_publish_to_web] || false @ article.game_source = @client_application create! do |success, failure| success.html {redirect_to(@article)} success.json {render :json => {:id=>@article.id, :created_at=>@article.created_at, :picture_urls=> @article.assets.map { |a| root_url.chop + a.photo.url}}} failure.html {render :action => […]

Rails STI和“type”字符串的设置

我想我需要在Rails中使用STI。 这是我的class级: class Person < ActiveRecord::Base end class Landlord < Person end 和people表有一个:type列,它是一个字符串。 所以,我期望在表中看到的是,作为Person的每一行都将类型设置为“Person”,并且每个Landlord都将类型设置为“Landlord”。 但是,这不是我所看到的。 每个房东的类型都设置为“Landlord”,但所有Person的类型都设置为nil。 这很好可能是rails的工作方式,但我只是在寻找一些确认。

inheritanceRails i18n子类中的validation错误消息

我明白了什么 假设我有一个方便的validation类,如: User {/regex/}, :message => :name_format end 在这种情况下,我可以使用i18n通过在/config/locals/en.yml包含以下内容来使错误消息可翻译: en: activerecord: errors: models: user: attributes: username: name_format: ‘has the way-wrong format, bro!’ 这很好,通常非常方便。 我想知道的是什么: 我的问题是:当我有从Userinheritance的子类时会发生什么: UserSubclassOne < User # extra stuff end UserSubclassTwo < User # extra stuff end … UserSubclassEnn < User # extra stuff end 现在的问题是Rails找不到翻译user_subclass_one.attributes.username.name_format 。 它抱怨说: translation missing: en.activerecord.errors.models.user_subclass_one.attributes.username.name_format 我希望Rails在UserSubclassOne中搜索字符串时会查找UserSubclassOne的层次结构,然后注意它何时获得“点击”,但是(除非我做了一些可怕的错误)显然不会发生了。 一个显而易见的解决方案是复制en.yml.en.errors.models中的user […]

单表inheritance路由?

我有单表inheritance适用于我的应用程序。 我有两个用户子类型(运动员和公司)inheritance了超级用户。 假设我列出了所有用户,并希望从此列表中链接到每个用户的个人资料。 我想链接到运动员控制器,如果类型是运动员,公司控制器,如果类型是公司。 是否有标准的Rails方式? 也许一些路由技巧?

Rails线程私有消息

我有以下两种型号: class Message ‘User’ belongs_to :from_user, :class_name => ‘User’ has_ancestry #Using the ‘ancestry’ gem end class User ‘Message’, :foreign_key => ‘to_user_id’ has_many :messages_sent, :class_name => ‘Message’, :foreign_key => ‘from_user_id’ end 允许每个用户与另一个用户进行一次对话,并且应该从原始消息中对所有回复进行线程化。 在我的“索引”控制器操作中,如何查询已发送的消息和收到的消息? 例如,如果User1命中’/ users / 2 / messages /’,他们应该看到user1和user2之间的整个对话(无论谁发送了第一条消息)。 我是否需要添加“线程”模型,或者有没有办法用我当前的结构来完成这个? 谢谢。

Rails named_scopeinheritance?

我试图通过提供一个inheritance的公共基础模型来概括我的一些模型,其包含一些相互的named_scope声明和一个filter方法,该方法激活该搜索以便在控制器端进行更简单的查询。 这在我在控制台中运行时似乎正在工作,但在控制器中失败时: # in the base model class GenericModel [ “#{self.table_name}.name like ?”, “%#{name}%” ] } } def filter(params) res = [] res = self.by_name( (params[:name] or ”) ) if params[:name] return res end end class MyModel > params = { :name => ‘jimmy’ } >> MyModel.filter(params) => [ , … ] nil # fails […]

嵌套属性可以与inheritance结合使用吗?

我有以下课程: 项目 人 人 > 开发人员 人 > 经理 在Project模型中,我添加了以下语句: has_and_belongs_to_many :people accepts_nested_attributes_for :people 当然还有Person类中的相应语句。 如何通过nested_attributes方法将Developer添加到Project ? 以下不起作用: @p.people_attributes = [{:name => “Epic Beard Man”, :type => “Developer”}] @p.people => [#] 如您所见, type属性设置为nil而不是”Developer” 。