Tag: 协会

用户,评论关联不起作用,comment.user.email返回No方法错误?

我有以下模型与下面给出的关联: – class Comment < ActiveRecord::Base belongs_to :post belongs_to :user end class Post < ActiveRecord::Base belongs_to :user has_many :comments end class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me has_many :posts has_many :comments end 但是,当我尝试访问评论的用户详细信息时,我没有获得方法错误:(。 浏览器中显示的错误如下: – undefined method `email’ for nil:NilClass 1: 2: 3: Comment written by: […]

如何在Rail 2.3.5中生成mongoid.yml配置?

正如标题所说,我如何在Rail 2.3.5上生成默认的mongoid.yml配置文件? 我尝试使用’rails generate mongoid:config’命令,但它只是生成一个新的应用程序。 而且,我想在mongoid中使用has_many而不在相同的字段中嵌入相关的模型。 我希望它们位于不同的字段中并通过* _id“列”关联。 那可能吗?

与has_many建立联系:通过多个模型

请帮助了解如何使用此树执行@project.payments : Project |__Stages |__Costs |__Payments project.rb has_many :stages has_many :costs, :through => stages stage.rb belongs_to :project has_many :costs has_many :payments :through => costs cost.rb belongs_to :stage has_many :payments payment.rb belongs_to :cost

has_many条件或proc对外键

我在两个模型之间有一个has_many关联,使用日期作为每个模型的外键和主键。 它完全可以单向工作,但不是另一种方式。 作品 has_one :quiz_log, :primary_key => :start_at, :foreign_key => :start_at 不起作用 has_many :event_logs, :primary_key => :start_at, :foreign_key => :start_at 原因是(我认为)因为QuizLog上的start_at是一个date而EventLog上的start_at是一个date 。 所以它返回nil试图匹配一个简单日期的确切日期时间。 如何在第二个语句上转换foreign_key start_at以将其从datetime首先转换为简单日期,以便它与第二个模型匹配?

Rails:如何访问新记录的嵌套关联的父模型?

假设我们有标准的Post&Comment模型,Post有accepts_nested_attributes_for :commments和:autosave => true set。 我们可以创建一个新post以及一些新评论,例如: @post = Post.new :subject => ‘foo’ @post.comments.build :text => ‘bar’ @post.comments.first # returns the new comment ‘bar’ @post.comments.first.post # returns nil 🙁 @post.save # saves both post and comments simultaneously, in a transaction etc @post.comments.first # returns the comment ‘bar’ @post.comments.first.post # returns the post ‘foo’ 但是,我需要能够在Comment(例如,来自其before_save或validation函数)之间进行区分 此评论未附加到post(无效) 此评论附加到未保存的post(有效) […]

Rails:通过关联创建直通记录

如果有人有更好的头衔,请告诉我:p 我有以下型号: class Car has_many :car_drivers has_many :drivers, :through => :car_drivers end class Driver has_many :car_drivers has_many :cars, :through => :car_drivers end class CarDriver belongs_to :car belongs_to :driver end 现在我想通过Car创建一个新的Driver,但是也应该创建join-table(car_drivers)中的记录。 我尝试了以下操作,但是在创建汽车记录时,连接表记录不是: driver_object.cars.create 在这种情况下,最佳做法是什么?

Rails 3. HABTM表单选择下拉菜单

我有发票表格。 这是一个简化版本:因此它包含行项目,您可以在其中选择产品名称的下拉菜单。 这很好用:所以invoice-line_item关系是这样的:invoice has_many line_items和line_item属于invoice。 line_item属于item和item has_many line_items。 我正确地设置了item,line_items和发票。 但现在我想为订单项添加税金。 所以我创建了一个line_items_taxes表来创建line_items和tax之间的HABTM关系。 但是我无法在表单中正确设置它。 我的表格看起来像这样…… |name|price|tax| | v| | v| | v| | v| | v| | v| [save invoice] 所以我需要一个TAXES下拉选择菜单,保存发票后,它会为每个订单项保存税金。 我已经尝试了http://snippets.dzone.com/posts/show/4369和Rails HABTM问题提供的解决方案,但是我收到了错误。 undefined方法合并为:name:Symbol ‘line_item[tax_ids][]’} %>

是否真的需要validation外键?

我正在使用Ruby on Rails v3.2.2,在发布我之前的问题之后 ,我想知道并理解是否(或不) 显式validation与ActiveRecord::Associations相关的外键是否需要。 例如: class CategoryAssociation ‘article_id’ belongs_to :category, :foreign_key => ‘category_id’ validates :article_id, :presence => true, :numericality => { :only_integer => true } validates :category_id, :presence => true, :numericality => { :only_integer => true } end 真的需要以上validates方法吗? 为什么?

Rails 4.1.0尝试从子表更新父表时没有方法错误

我有2个rails模型,一个security和stock_quote模型,如下所示 class StockQuote < ActiveRecord::Base belongs_to :security, class_name: "Security", foreign_key: 'security_id' def self.price_on(date) where("date(created_at) = ?", date).sum(:high) end feed_url = "https://spreadsheets.google.com/feeds/list/1ncyK8uXoeLobVkdiSKQcYJr2joK_uN5QSBB3814GKaw/od6/public/values" def self.update_from_feed(feed_url) feed = Feedjira::Feed.fetch_and_parse(feed_url) unless feed.is_a?(Fixnum) add_entries(feed.entries) else puts "not an array\n" end end private def self.add_nonexistent_security(entry) a = StockQuote.create_security(security: entry.title.capitalize, category: "Uncategorized") end def self.save_entry(entry,security_id) unless exists? guid: entry.id contents = entry.content.split(',').map […]

Rails关联 – 改变值的问题,以及过多的缓存!

假设我有一个卡片游戏应用程序,其中包含一个Player模型,它有一个actions整数列; 和Card模型。 玩家可以玩他们拥有的牌,这需要一个动作; 一张特定的牌在播放时会发出两个动作。 如果我按如下方式编码: class Player < ActiveRecord::Base has_many :cards def play_card(card) raise "Not yours!" unless cards.include? card self.actions -= 1 card.play save! end end class Card < ActiveRecord::Base belongs_to :player def play player.actions += 2 end end …然后, Player#play_card的净效果是将actions减少1.我发现两个更改的唯一方法适用于同一个对象,从而导致1个操作的净增量,是定义函数像这样: class Player < ActiveRecord::Base has_many :cards def play_card(card) raise "Not yours!" unless cards.include? card […]