非常简单的问题 - 我如何进行搜索以查找名称以ActiveRecord中的某个字符串开头的所有记录.我已经在互联网上看到了各种各样的比特,其中使用了逐字的LIKE SQL子句 - 但是从我所听到的那些不是'正确'的方式.
有没有'正确'的Rails方式?
我有一个Foo属性模型id, name, location.我有一个例子Foo:
f1 = Foo.new
f1.name = "Bar"
f1.location = "Foo York"
f1.save
Run Code Online (Sandbox Code Playgroud)
我想复制f1并从该副本创建另一个Foo模型实例,但我不想f1.id继续f2.id(我不想明确指定它,我希望数据库处理它,因为它应该) .
除了手动复制每个属性之外,还有一种简单的方法吗?任何内置函数或写一个是最好的路线?
谢谢
我有一个模型关系,其中today有很多tasks
我正在尝试检索用户的today对象,包括tasks并将它们全部呈现给Json.所有这一切都很顺利,直到我决定要tasks在today对象内进行排序,因为respond_with block它也用于渲染html页面.有没有办法包括tasks和订购它们?
我正在尝试这样的事情:
class TodaysController < ApplicationController
respond_to :html, :json
def show
@today = Today.where(:user_id => current_user.id).joins(:tasks).includes(:tasks).order(:priority).first
respond_with @today, :include => :tasks
end
end
Run Code Online (Sandbox Code Playgroud)
这会正确检索所有内容,但似乎根本不会对任务进行排序.
这就是我曾经拥有的(效果很好,但没有订购):
class TodaysController < ApplicationController
respond_to :html, :json
def show
@today = current_user.today
respond_with @today, :include => :tasks
end
end
Run Code Online (Sandbox Code Playgroud)
我知道我可以检索数据并在之后对其进行排序,如下所示:
@today = current_user.today
@today.tasks.sort!{|a,b| a.priority <=> b.priority }
Run Code Online (Sandbox Code Playgroud)
这有效并将通过我的测试,但我希望有一种ActiveRecord方法来解决这个问题.
我正在使用多态关联来跟踪我的项目中的注释.所有非常直接的东西.
我遇到的问题是基于多态关联查询并从Comment模型加入到它的所有者.
所以......
我有一个评论模型
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
和ForumTopics模式:
class ForumTopic < ActiveRecord::Base
has_many :comments, :as => :commentable
end
Run Code Online (Sandbox Code Playgroud)
我还有其他几个"可评论"的模型,现在并不重要.所有这一切都有效.
我想要做的是找到属于具有指定条件的ForumTopic的所有注释(在这种情况下,'featured'== true).
当我尝试使用finder加入模型时:
@comments = Comment.find(:all
:joins => :commentable
:conditions => ["forum_topics.featured = ? ", true]
)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
无法急切加载多态关联:可评论
使用AR"包含语法":
@comments = Comment.find(:all
:include => :forum_topics
:conditions => ["forum_topics.featured = ? ", true]
)
Run Code Online (Sandbox Code Playgroud)
收益:
未找到名为"forum_topics"的协会; 也许你拼错了吗?
如果我尝试使用表名而不是关联名(字符串而不是符号)加入:
@comments = Comment.find(:all,
:joins => "forum_topics",
:conditions => ["forum_topics.featured = ? ", true]
)
Run Code Online (Sandbox Code Playgroud)
我知道了:
Mysql …
用户只能编辑自己的帖子,因此我使用以下内容检查用户是否可以输入编辑表单:
def edit
@post = Load.find(:first, :conditions => { :user_id => session[:user_id], :id => params[:id]})
rescue ActiveRecord::RecordNotFound
flash[:notice] = "Wrong post it"
redirect_to :action => 'index'
end
Run Code Online (Sandbox Code Playgroud)
但它不起作用,任何想法我做错了什么?
我需要一个包含表名列的数组
有关如何使用rails 3.0.0rc执行此操作的任何想法?
A Person可以有很多Events,每个都Event可以有一个多态Eventable记录.如何指定记录Person与Eventable记录之间的关系?
以下是我的模型:
class Event < ActiveRecord::Base
belongs_to :person
belongs_to :eventable, :polymorphic => true
end
class Meal < ActiveRecord::Base
has_one :event, :as => eventable
end
class Workout < ActiveRecord::Base
has_one :event, :as => eventable
end
Run Code Online (Sandbox Code Playgroud)
主要问题涉及到Person班级:
class Person < ActiveRecord::Base
has_many :events
has_many :eventables, :through => :events # is this correct???
end
Run Code Online (Sandbox Code Playgroud)
我是否has_many :eventables, :through => :events像上面那样说?
或者我必须像这样拼写它们:
has_many :meals, :through => :events
has_many :workouts, …Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails has-many-through polymorphic-associations
有没有办法用ActiveRecord获取实际的列名?
当我使用连接调用find_by_sql或select_all时,如果存在具有相同名称的列,则第一个被覆盖:
select locations.*, s3_images.* from locations left join s3_images on s3_images.imageable_id = locations.id and s3_images.imageable_type = 'Location' limit 1
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我得到以下内容:
#<Location id: 22, name: ...
>
Run Code Online (Sandbox Code Playgroud)
其中id是最后一个s3_image的id.select_rows是唯一按预期工作的东西:
Model.connection.select_rows("SELECT id,name FROM users") => [["1","amy"],["2","bob"],["3","cam"]]
Run Code Online (Sandbox Code Playgroud)
我需要获取上面行的字段名称.这篇文章接近我想要的但看起来过时了(fetch_fields似乎不再存在你如何获得ActiveRecord查询结果中的行和列?)
ActiveRecord连接方法创建多个对象.我正在尝试实现相同的结果"包含"将返回但是使用左连接.
我试图返回大量的结果(有时整个表格),这就是为什么包含不适合我的需要.
我有一个简单的查询需求:查找自2013年1月1日起订购的用户列表.
在SQL中,这是一个非常简单的查询.
但我正在使用Rails和Active Record.
所以我写道: User.joins(:orders).where("orders.created_at >= '2013-01-01 00:00:00'")
在我们的数据库中,我们自2013年1月1日起有75个用户发出了100个订单.(显然有些用户发了多个订单.)
但是,上面的表达式返回100个用户.(必须有重复.)
我试过了 User.joins(:orders).where("orders.created_at >= '2013-01-01 00:00:00'").uniq
这也行不通.
如何获得自2013年1月1日起订购的75位用户?
我想从我的Rails应用程序中将几千条记录批量插入数据库(在我的情况下为POSTGRES).
这样做的"Rails方式"是什么?一些快速且正确的方法.
我知道我可以通过字符串连接属性来创建SQL查询,但我想要一个更好的方法.
ruby activerecord ruby-on-rails ruby-on-rails-3 rails-postgresql