Tag: 型号

Rails – 模型疑问

鉴于我有这样的模型: class Person has_many :owned_groups, :class_name => “Group”, :foreign_key => :owner_id has_many :owned_group_memberships, :through => :owned_groups, :source => :group_memberships has_many :group_memberships, :foreign_key => “member_id” has_many :groups, :through => :group_memberships end class GroupMembership belongs_to :member, :class_name => ‘Person’ belongs_to :group end class Group belongs_to :owner, :class_name => “Person” has_many :group_memberships has_many :members, :through => :group_memberships end […]

从另一个访问一个模型

我有一个执行算法的模型方法,还有另一个模型,在保存之后,在某些情况下,需要刷新算法结果。 所以,我希望能够做到这样的事情: class Model1 < ActiveRecord::Base after_save :update_score def update_score if … … else # run_alg from class Model2 end end end class Model2 < ActiveRecord::Base def run_alg … end end 这是可能的还是我必须将run_alg移动/复制到application.rb中?

在rails 3中动态获取模型属性

如何动态地从模型对象获取属性? 我将User对象的属性作为字符串: u = User.find(1) 我可以做像u.get(“user_id”)这样的事情u.get(“user_id”)

重定向捕获模型中方法中的exception

我使用Authlogic-connect连接各种服务提供商。 user.rb中有一个方法 def complete_oauth_transaction token = token_class.new(oauth_token_and_secret) old_token = token_class.find_by_key_or_token(token.key, token.token) token = old_token if old_token if has_token?(oauth_provider) self.errors.add(:tokens, “you have already created an account using your #{token_class.service_name} account, so it”) else self.access_tokens << token end end 当添加了服务提供者时,它会给出has_token中所述的错误? 方法和分页符。 我需要将应用程序重定向到同一页面并闪烁错误。 我该怎么做呢? 我已经在我自己的user.rb中覆盖了该方法,以便我可以更改代码。

Rails – 一种forms到两种模式

我正在Rails(版本4.2.5)中构建一个表单来收集有关商店的基本信息,我希望将表单提交给两个单独的模型 – 一个名为Store,它收集有关商店的信息,一个名为Address ,它只保存地址信息。 我已经看到这个问题浮在Stack和其他地方,但我对Rails很新,可以使用更多的逐步指导。 (另外,我知道Rails地理编码器的gem,但不想在这个项目中使用它,尽管我正在收集地址和纬度/长信息) 对于初学者,这是我的商店和地址模型…… class Store < ActiveRecord::Base belongs_to :user belongs_to :store_type has_one :address end class Address < ActiveRecord::Base belongs_to :store end 和我的StoresController …… class StoresController < ApplicationController def new @store = Store.new @storeType = StoreType.all end def create @store = Store.new(store_params) @store.user = current_user if @store.save flash[:notice] = "New store created" […]

如何从模型中调用辅助方法?

我在MongoID gem的Rails应用程序中使用MongoDB作为数据库。 我想用n aafter_create回调方法从模型中调用helper方法。 这怎么可能? 我的型号代码是: class Department include ApplicationHelper after_create :create_news private def create_news @user = ApplicationHelper.get_current_users end end 我的助手代码是: module ApplicationHelper def get_current_users current_user end end 当我创建新部门时,会发生以下错误。 undefined method `get_current_users’ for ApplicationHelper:Module 如何删除错误? 提前致谢。

使用委托使用has_many在Rails中?

我们有2个型号和一个连接模型: #app/models/message.rb Class Message < ActiveRecord::Base has_many :image_messages has_many :images, through: :image_messages end #app/models/image.rb Class Image < ActiveRecord::Base has_many :image_messages has_many :messages, through: :image_messages end #app/models/image_message.rb Class ImageMessage < ActiveRecord::Base belongs_to :image belongs_to :message end 额外属性 我们希望从连接模型( ImageMessage )中提取额外的属性,并在Message模型中访问它们: @message.image_messages.first.caption # -> what happens now @message.images.first.caption #-> we want 我们已经在声明关联时使用select方法实现了这一点: #app/models/message.rb has_many :images, -> { […]

Rails中的自引用模型3

我有一个实体模型,我想显示实体之间的连接。 即,实体1连接到实体2。 我现在的想法是在两个名为Connection之间创建一个连接模型,让它像传统的rails连接表一样工作。 除了列为entity_one_id和entity_two_id之外,然后在Entity和Connection之间建立多对多关系。 这似乎是一种非常优雅的方式。 我想知道是否有人有更好的想法? 也许更多的东西,我只是没有看到?

Rails与模型和迁移相关联

无法理解Rails如何与模型联系起作用。 这是一个简单的问题: 有两张桌子 制品 id| name | status 1 | Tube | 0 2 | Pillar | 1 3 | Book | 0 4 | Gum | 1 5 | Tumbler | 2 状态 status | name 0 | Unavailable 1 | In stock 2 | Discounted 与模型和控制器相同的名称。 我不知道在每个新产品的状态表中创建新行的内容。 我想在erb中显示状态名称。 我应该在模型和迁移文件中写什么(例如,belongs_to,has_one或has_many ……)?

奇怪的问题has_many通过更新Rails中的关联

我从Ruby 2.1.2更新到Ruby 2.2.0并更新了链接到它的所有gem。 我有几个模型在彼此之间相互联系 class Question < ActiveRecord::Base belongs_to :course_version has_one :course, through: :course_version has_many :answer_questions has_many :answers, through: :answer_questions end class AnswerQuestion < ActiveRecord::Base belongs_to :answer belongs_to :question end class Answer < ActiveRecord::Base has_many :answer_questions has_many :questions, through: :answer_questions end 如您所知,我们有问题可以得到答案并通过answer_questions来了解他们得到了什么。 在我更新Ruby之前,它工作得很好。 现在我做的事情…… my_question.answers << my_answer 它确实爆炸了 NoMethodError: undefined method `name’ for nil:NilClass /Users/Loschcode/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:80:in […]