我正在使用acts_as_commentable_with_threading gem.这个gem包含方法'comment.children',它创建所有子注释的哈希值.默认情况下,它通过created_at ASC对子项进行排序.我想改变孩子的订购方式,但据我所知,我不能直接编辑这个方法.相反,我一直试图在视图中对它们进行重新排序,如下所示:
<% @comments = comment.children.order('created_at DESC') %>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这没有任何效果.知道我做错了什么吗?
我有两个连接表连接的模型:
class Publication < ActiveRecord::Base
attr_accessible :title, :author_attributes, :translator_attributes
has_many :publication_contributors
has_many :authors, :through => :publication_contributors, :source => :contributor,
:conditions => {:publication_contributors => {:contributor_type => "Author"}}
has_many :translators, :through => :publication_contributors, :source => :contributor,
:conditions => {:publication_contributors => {:contributor_type => "Translator"}}
accepts_nested_attributes_for :authors, :translators
end
class Contributor < ActiveRecord::Base
attr_accessible :name
has_many :publications, :through => :publication_contributors
has_many :publication_contributors
end
class PublicationContributor < ActiveRecord::Base
attr_accessible :contributor_type
belongs_to :publication
belongs_to :contributor
end
Run Code Online (Sandbox Code Playgroud)
请注意,连接表具有第三个属性(在publication_id和contributor_id旁边),称为contributor_type.此属性可能包含诸如"作者","翻译者","编辑者"或其他内容的角色.在我的出版物模型中,我为两个更常见的contributor_types创建了一对关联,"Author"和"Translator".这些关联可以很好地检索相关数据,例如@publication.authors
.但是,当通过嵌套形式创建这些关联时,它会溅射.
我的表单看起来像这样:
<%= form_for @publication, :remote => true …
Run Code Online (Sandbox Code Playgroud) 将宏添加到 Rspec 的配置时,您必须指定它将被访问的测试类型。例如,您可以键入:
config.extend ControllerMacros, :type => :controller
Run Code Online (Sandbox Code Playgroud)
你如何让它与 Capybara 一起工作,它的类型 (:feature) 似乎不被Rspec 的 config识别。尝试这样的事情是行不通的:
config.extend FeatureMacros, :type => :feature
Run Code Online (Sandbox Code Playgroud) 有没有办法将 ETH 添加到 Ganache 帐户?我知道我通常可以通过重新启动 ganache cli 来刷新帐户,但我正在使用该--db
选项,这意味着帐户是持久的。因此,它们很快就干涸了。
我有两个模型,每个模型都有自己的Carrierwave上传器:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
Run Code Online (Sandbox Code Playgroud)
和:
class Bookshelf < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
Run Code Online (Sandbox Code Playgroud)
我希望用户的头像是他上传的最新书架图像.我尝试这样做:
class BookcasesController < ApplicationController
def create
@bookcase = current_user.bookcases.build(params[:bookcase])
if @bookcase.save
current_user.avatar = @bookcase.image
current_user.avatar.recreate_versions!
end
end
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,这对头像完全没有影响.我怎么可能做到这一点?
我有以下方法创建并执行SQL查询:
def edition_authors(edition, authors)
query_string = "contributor_type = ? AND author = ?"
for i in 1..authors.length - 1
query_string += " OR author = ?"
end
return edition.contributors.where(query_string, 'Author', authors)
end
Run Code Online (Sandbox Code Playgroud)
最后一行是我遇到麻烦的那一行.我希望'authors'数组以某种方式变成一组字符串.例如,如果作者数组包含['James Joyce','Cory Doctorow','Cormac McCarthy'],我希望最后一行如下:
return edition.contributors.where(query_string, 'Author', 'James Joyce', 'Cory Doctorow', 'Cormac McCarthy')
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
在我的网站上,版主可以标记垃圾评论.标记这些注释后,它们会被隔离,因此它们不再出现在常规视图中,但仍可在管理控制面板中看到它们.目前,我将它们排除在常规视图之外:
@comments = Comment.where(:flagged => false)
Run Code Online (Sandbox Code Playgroud)
我在每个有注释的控制器中执行此操作,其中有许多注释.我觉得在Rails中有一个更清晰的方法来处理这个问题.也许在评论模型中的某处我可以指定在查询注释时,只检索那些未标记的注释.如果是这样,那怎么办?即使这是不可能的,还有其他方法来干这个代码吗?
我有两个型号:
class Forum < ActiveRecord::Base
has_many :threads
end
class Thread < ActiveRecord::Base
belongs_to :forum, :dependent => :destroy
has_many :posts
end
Run Code Online (Sandbox Code Playgroud)
在我看来,我试试这个:
<%= @forum.name %>
<% @forum.threads.each do |thread| %>
<%= link_to thread.name, [@forum, thread], :class => "remote" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我在第1行遇到了这个错误:
undefined method `scoped' for Thread:Class
Run Code Online (Sandbox Code Playgroud)
知道这里发生了什么吗?我已经看到其他用户描述了这个问题,但这通常是因为他们将has_many复数化或者将belongs_to统一化.据我所知,我的模型制作精良.请让我知道我做错了什么.
完整堆栈跟踪:
activerecord (3.2.9) lib/active_record/associations/association.rb:123:in `target_scope'
activerecord (3.2.9) lib/active_record/associations/association.rb:87:in `scoped'
activerecord (3.2.9) lib/active_record/associations/collection_association.rb:382:in `find_target'
activerecord (3.2.9) lib/active_record/associations/collection_association.rb:335:in `load_target'
C:in `load_target'
activerecord (3.2.9) lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
app/views/forums/_show.html.erb:2:in `_app_views_forums__show_html_erb___1062139204_27502608'
actionpack (3.2.9) lib/action_view/template.rb:145:in `block in render'
activesupport …
Run Code Online (Sandbox Code Playgroud) 据我所知,验证假设在回调之前运行.不过,我的行为表明不然.具体来说,我有:
class User::GroupInvitation < ActiveRecord::Base
validate :user_can_be_invited
before_create :find_or_create_membership
private
def user_can_be_invited
user_membership = User::Membership.where(:group_id => self.group_id, :user_id => self.invitee_id).first
if user_membership.present?
case user_membership.status
when "invited"
errors[:base] << I18n.t("user.group_invitations.create.this_user_is_already_invited")
end
end
end
def find_or_create_membership
user_membership = User::Membership.where(:group_id => self.group_id, :user_id => self.invitee_id).first_or_create(:status => "invited")
user_membership.update_column(:status, "invited") unless user_membership.new_record?
self.user_membership_id = user_membership.id
end
end
Run Code Online (Sandbox Code Playgroud)
测试此代码时,我始终会收到错误"此用户已被邀请",即使用户之前未被邀请也是如此.我最好的猜测是,这种情况正在发生,因为find_or_create_membership
它首先运行,因此设置status
为invited
.关于正在发生什么或如何解决这种情况的任何想法?
我有一个叫做多态的模型Update
.通常,我可以updatable
使用简单的方法呈现其关联模型:
<%= render @update.updatable %>
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果我从命名空间控制器中调用此方法,那么Rails也会尝试命名视图路径,admin/reviews/review
而不是搜索reviews/review
.这导致如下错误:
Missing partial admin/reviews/review
Run Code Online (Sandbox Code Playgroud)
通常我可以硬编码变通方法,例如:
<%= render :partial => "/reviews/review", :locals => {:review => @update.updatable}
Run Code Online (Sandbox Code Playgroud)
如果关联不是多态的,那就没关系,但既然如此,如果updatable
不是a ,我会得到错误Review
.不幸的是,我有几十个可能的updatable
s,并通过case
声明进行分支将很难维护.
我有没有想到更简单的方法?
我有以下.travis.yml
文件:
language: ruby
rvm:
- "2.0.0"
# uncomment this line if your project needs to run something other than `rake`:
before_script:
- psql -c "create database dummy_test;" -U postgres
- psql -c "CREATE USER dummy WITH PASSWORD 'dummy';" -U postgres
script:
- RAILS_ENV=test bundle exec rake db:migrate --trace
- bundle exec rake db:test:prepare
- bundle exec rspec spec
Run Code Online (Sandbox Code Playgroud)
当我尝试在Travis CI中运行它时,我收到以下错误消息:
$ RAILS_ENV=test bundle exec rake db:migrate --trace
rake aborted!
Don't know how to build task 'db:migrate'
Run Code Online (Sandbox Code Playgroud)
几个小时我一直在尝试不同的方法.我究竟做错了什么?
不幸的是,ActiveModel :: Serializers目前不支持验证错误,尽管它们被安排为1.0.在那之前,我必须破解我自己的解决方案.大问题?我不知道Ember Data的ActiveModelAdapter期望这些错误的格式是什么.我试过简单地传递errors
属性,但是Ember Data没有接受它:
class MySerializer < ActiveModel::Serializer
attributes :errors
end
Run Code Online (Sandbox Code Playgroud)
那么我该怎么办呢?
activerecord ×1
callback ×1
capybara ×1
carrierwave ×1
config ×1
ember-data ×1
ember.js ×1
ethereum ×1
jointable ×1
mysql ×1
namespaces ×1
nested-forms ×1
rspec ×1
ruby ×1
travis-ci ×1
validation ×1
view ×1