小编sla*_*nty的帖子

Rails:ActiveRecord :: HasManyThroughSourceAssociationNotFoundError:找不到源关联

我有以下代码(有点简化......

create_table :signatures do |t|
  t.integer :signer_id
  t.integer :card_id

  t.timestamps
end
Run Code Online (Sandbox Code Playgroud)

随着模型看起来像......

class Signature < ActiveRecord::Base
    belongs_to :card
    belongs_to :user
end

class Card < ActiveRecord::Base
    has_many :signatures
    has_many :signers, :through => :signatures, :foreign_key => "card_id"
end


class User < ActiveRecord::Base

    has_many :sent_cards, :class_name => "Card", :foreign_key => "sender_id"
    has_many :received_cards, :class_name => "Card", :foreign_key => "recipient_id"

    has_many :signatures
    has_many :signed_cards, :through => :signatures, :foreign_key => "signer_id"

end
Run Code Online (Sandbox Code Playgroud)

我使用rails控制台看到以下错误...

ruby-1.9.2-p0 > u15.signed_cards
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :signed_card or …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails associations has-many-through

13
推荐指数
2
解决办法
5487
查看次数

Rails 模型测试 - 模拟与工厂

Rails 测试模拟对象与使用工厂对象的最佳实践是什么?当模型可能转至外部源时才应使用模拟吗?或者,您是否仅在测试实际模型并使用模拟来处理其他所有内容时才使用工厂。

例如,如果我们有一个包含客户和订单的销售系统,当我们测试客户模型时,我们是模拟订单还是只使用工厂订单?它甚至有什么区别吗?

rspec ruby-on-rails factory-bot

5
推荐指数
1
解决办法
1147
查看次数

Rails - grouped_collection_select

我正在尝试使用以下内容获取grouped_collection_select

class User
  has_many :pages, :through => pages_users
end

class Page
  has_many :users, :through => pages_users
  # name - String
  # type - String
end

class PagesUser < ActiveRecord::Base
  belongs_to :page 
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我希望下拉列表按类型分组,名称在下面.我在城市/乡村/大陆看过的例子并不像我想的那样有用.这样做的最佳方法是什么?我想我想要......

<%= grouped_collection_select(:user, :page_id, user.pages, :type, :name, ) %>
Run Code Online (Sandbox Code Playgroud)

但这显然是不正确的.

有什么想法吗?

编辑通过pages_users表显示实际关系.

ruby forms ruby-on-rails

2
推荐指数
1
解决办法
2779
查看次数

Rails数据库关联/迁移

我正在学习rails,我正试图弄清楚数据库关联.如果我有一个数据库表,其中包含具有id,姓名,电子邮件等的用户以及带有消息的Message表,发件人(User)和收件人(也是User),如何设置迁移和楷模.在这里,我使用的是Rails 3.1.我很确定我可以在Message表中只有一个用户使用引用:迁移中的User,但我不确定如何设置其中两个.

model database-migration ruby-on-rails-3.1

1
推荐指数
1
解决办法
2491
查看次数

可以推送到Heroku但无法运行迁移

我可以将Rails应用程序推送到Heroku ... git push heroku 但是当我尝试迁移时...... heroku run rake db:migrate 我得到......

Running rake db:migrate attached to terminal... failed ! You do not have access to the app my-app-name-1234.

编辑:我的.git/config包含... [remote "heroku"] url = git@heroku.com:young-mist-1198.git fetch = +refs/heads/*:refs/remotes/heroku/*

有任何想法吗?在Heroku方面看起来很合理,似乎我的ssh键坏了,它甚至不会让我推.

rake heroku dbmigrate ruby-on-rails-3

1
推荐指数
1
解决办法
1108
查看次数