Tag: activerecord

Ruby on Rails通过自我引用的跟随/关注者关系

has_many上有很多post和post:通过,但我没有找到任何具体涵盖我正在尝试做的事情。 我有一个用户模型和一个友谊模型。 用户有许多跟随他们的用户,以及许多关注者。 这是通常的Twitter模型。 对于给定用户,我想设置Active Record关系,该关系返回跟随用户并且用户是其关注者的实际用户。 这些是我设置的关系: class User ‘User’, :through => :friendships, :foreign_key => ‘user_id’ has_many :followers, :class_name => ‘User’, :through => :friendships, :foreign_key => ‘friend_id’ end class Friendship ‘User’, :foreign_key => ‘friend_id’ belongs_to :follower, :class_name => ‘User’, :foreign_key => ‘user_id’ end 以下关系有效 – 它生成以下连接: SELECT `users`.* FROM `users` INNER JOIN `friendships` ON `users`.id […]

Rails与activerecord共享会话

我目前正在使用默认cookie作为我的单点登录(SSO),但是一些用户在推送更新后出现了奇怪的错误。 我正在考虑转移到活动记录来存储会话但是我想知道我如何告诉rails会话在另一个数据库中? 因此,如果我通过AR在App1DB中存储会话,那么所有其他应用程序如何知道在哪里查找会话?

Ruby on Rails使用连接进行ActiveRecord查询

我有一个用户模型,用户有一个has_many宠物的关系。 我希望能够编写一个ActiveRecord查询,我可以选择所有用户没有pet.name“蓬松”的用户 使用ActiveRecord写这个的最有效方法是什么? 使用直接SQL它会看起来如下所示: select id from users INNER JOIN pets ON u.id = pets.user_id WHERE pets.name != “fluffy”

rails,activerecord,获取当前连接规范

我正在编写代码,将一些数据从一个数据库迁移到另一个数据库,覆盖目标中的一些数据。 它使用ActiveRecord,因为它已经与使用AR的Rails应用程序相关联。 由于某些数据将被覆盖,我想提供一个确认提示,告诉用户用于目标数据库连接的实际连接字典/规范。 你知道,适配器,主机,用户名,密码,数据库,你在database.yml中列出的东西。 我可以将模型用于我正在编写的内容并要求SomeModel.connection ….但似乎根本没有API来从实时连接对象中获取实际的连接规范。 真? 我错过了什么吗? 还有其他任何想法,甚至是无证的api?

在Rails中关联两个模型(用户和配置文件)

我是Rails的新手。 我正在构建一个具有用户模型和配置文件模型的应用程序。 我想将这些模型关联起来: – 用户创建帐户后,他会自动发送到“创建个人资料”页面,他创建的个人资料仅连接到该特定用户。 – 只有拥有该配置文件的用户才能对其进行编辑。 我使用nifty_generators生成了用户模型。 当用户点击提交创建帐户时,我会将其重定向到“新配置文件”视图以创建配置文件。 我是通过编辑用户控制器中的重定向路径来完成的。 用户控制器如下所示: def create @user = User.new(params[:user]) if @user.save session[:user_id] = @user.id flash[:notice] = “Thank you for signing up! You are now logged in.” redirect_to new_profile_path else render :action => ‘new’ end end 这是有效的,但问题是应用程序似乎没有意识到该配置文件已连接到该特定用户。 我能够创建配置文件,但配置文件和用户之间似乎没有关系。 我的个人资料模型列表:belongs_to:user 我的用户模型列表:has _one:profile 我的routes.rb文件列出了以下内容: map.resources:users,:has_one =>:profile map.resources:个人资料 我在配置文件表中有一个user_id外键。 我的架构如下所示: create_table “profiles”, […]

迁移不在Heroku上工作

我在Heroku上运行pg:reset并尝试运行db:migrate ,所有迁移都会运行,但迁移失败并出现以下错误和跟踪: rake aborted! Error dumping database /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/tasks/postgresql_database_tasks.rb:55:in `structure_dump’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/tasks/database_tasks.rb:142:in `structure_dump’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:288:in `block (3 levels) in ‘ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:51:in `block (2 levels) in ‘ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:45:in `block (2 levels) in ‘ 从这里可以看出,问题线和上面的线是: command = “pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration[‘database’])}” raise ‘Error dumping database’ unless Kernel.system(command) 这在本地工作,在开发和生产环境中都没有任何问题。 有没有人经历过这样的事情?

使用has_many:through时,连接模型中的validation失败

我的完整代码可以在https://github.com/andyw8/simpleform_examples上看到 我有一个带有以下validation的连接模型ProductCategory : validates :product, presence: true validates :category, presence: true 我的Product型号具有以下关联: has_many :product_categories has_many :categories, through: :product_categories 当我尝试使用类别创建新产品时,调用@product.save! 在控制器中失败: Validation failed: Product categories is invalid 当我删除validation时,一切正常,连接模型正确保存。 我正在使用strong_parameters但我认为不应该与此问题相关。

在Rails查询中包含LIKE子句的最佳方法是什么?

在Rails查询中包含LIKE子句的最佳方法是什么,即(完全不正确的): Question.where(:content => ‘LIKE %farming%’)

如何让rails以正确的数据类型而不是字符串返回SUM(columnName)属性?

假设查询以下表单 operatingExpenses = Expense.find(:all, {:select=>”categories.activityType, categories.name heading, sum(amount) totalAmount”, :joins => “inner join expense_categories categories on category_id = categories.id “, :group => “categories.activityType, categories.name”, :order => “categories.activityType, totalAmount DESC”} ) 现在,amount被定义为数据库模式中的十进制字段。 例如,Rails迁移中的定义是 create_table :expenses do |table| table.column :created_at, :timestamp, :null=>false table.column :description, :string, :limit=>100, :null=>false table.column :amount, :decimal, :scale=>2, :precision=>10, :null=>false table.column :category_id, :integer, {:null=>false, […]

如何通过连接表填充has_many中的字段

我有一个关于活动记录关联的问题,请参考rails文档的这一部分: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association 如果我们有三个型号: class Physician :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient :appointments end 文档说可以通过api以这种方式管理连接模型的集合: physician.patients = patients 但是,如果约会模型(如链接示例中)有一个名为appointment_date的字段,并且我想在特定日期为医生和患者创建新约会,该怎么办? 以下代码将在约会表中创建一条记录,但是如何在第三步中填充appointment_date呢? physician = Physician.first patient = Patients.first physician.patients << patient 做这样的事情存在吗? physician.patients.create( :patient => patient, ‘appointment.appointment_time’ => appointment_time )