activerecord

Prepare and execute statements with ActiveRecord using PostgreSQL

佐手、 提交于 2020-01-03 13:04:04
问题 I am trying to insert values via a prepared statement using ActiveRecord. However, everytime I try: conn = ActiveRecord::Base.connection conn.prepare "SELECT * from sampletable where id = $1" conn.execute 3 After the second statement, I get: NoMethodError: undefined method `prepare' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x000001027442c8> What should I do? I'm running Rails 3.2.1 and Ruby 1.9.2 UPDATE: I solved the problem. Thanks for the response, but it didn't work for

Prepare and execute statements with ActiveRecord using PostgreSQL

穿精又带淫゛_ 提交于 2020-01-03 13:01:37
问题 I am trying to insert values via a prepared statement using ActiveRecord. However, everytime I try: conn = ActiveRecord::Base.connection conn.prepare "SELECT * from sampletable where id = $1" conn.execute 3 After the second statement, I get: NoMethodError: undefined method `prepare' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x000001027442c8> What should I do? I'm running Rails 3.2.1 and Ruby 1.9.2 UPDATE: I solved the problem. Thanks for the response, but it didn't work for

How to share activerecord models via Ruby Gem

筅森魡賤 提交于 2020-01-03 12:35:25
问题 I have a couple of Rails project which have some codebase in common. The common code consists of some ActiveRecord Models and an api on top of that. Currently I am duplicating the common code in all projects which is a very bad practice. Now I want to move the common code to a Ruby Gem. I am new to Ruby on Rails. I have looked at several Gem tutorials but could not find anything useful that will help me create a gem with reusable ActiveRecord models. Essentially I want is that - Gem contains

How to share activerecord models via Ruby Gem

廉价感情. 提交于 2020-01-03 12:35:04
问题 I have a couple of Rails project which have some codebase in common. The common code consists of some ActiveRecord Models and an api on top of that. Currently I am duplicating the common code in all projects which is a very bad practice. Now I want to move the common code to a Ruby Gem. I am new to Ruby on Rails. I have looked at several Gem tutorials but could not find anything useful that will help me create a gem with reusable ActiveRecord models. Essentially I want is that - Gem contains

How do I reuse connections in ActiveRecord?

陌路散爱 提交于 2020-01-03 12:23:09
问题 I was playing around with Sinatra and ActiveRecord when I found that a connections are not reused automatically by this simple API. #!/usr/bin/env ruby require 'sinatra' require 'active_record' ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: 'newsletter.db' ) ActiveRecord::Schema.define do create_table :subscribers do |t| t.string :email t.timestamps end end class Subscriber < ActiveRecord::Base validates :email, presence: true end class Newsletter < Sinatra::Base set

SQL join in CodeIgniter with Active Record

佐手、 提交于 2020-01-03 11:04:17
问题 I'm trying to wrap my head around this, but I seem to go in circles. I'm trying to list a users topics one by one, with the quotes belonging to that specific topic underneath. If that makes sense. I have 3 tables, like so: [USERS] user_id username [TOPICS] topic_id user_id topic_name [QUOTES] quote_id topic_id quote_name I want to be able to do something like this in my view: Username: Thomas Topic 1: Whatever Quotes: One quote, another quote, and a third quote, all belonging to Topic 1.

SQL join in CodeIgniter with Active Record

这一生的挚爱 提交于 2020-01-03 11:04:08
问题 I'm trying to wrap my head around this, but I seem to go in circles. I'm trying to list a users topics one by one, with the quotes belonging to that specific topic underneath. If that makes sense. I have 3 tables, like so: [USERS] user_id username [TOPICS] topic_id user_id topic_name [QUOTES] quote_id topic_id quote_name I want to be able to do something like this in my view: Username: Thomas Topic 1: Whatever Quotes: One quote, another quote, and a third quote, all belonging to Topic 1.

ActiveRecord, find by polymorphic attribute

扶醉桌前 提交于 2020-01-03 09:11:51
问题 Having this: class Event < ActiveRecord::Base belongs_to :historizable, :polymorphic => true end user = User.create! I can: Event.create!(:historizable => user) But I can't: Event.where(:historizable => user) # Mysql2::Error: Unknown column 'events.historizable' in 'where clause' I have to do this instead: Event.where(:historizable_id => user.id, :historizable_type => user.class.name) Update Code that reproduces the issue: https://gist.github.com/fguillen/4732177#file-polymorphic_where_test

ActiveRecord, find by polymorphic attribute

故事扮演 提交于 2020-01-03 09:11:06
问题 Having this: class Event < ActiveRecord::Base belongs_to :historizable, :polymorphic => true end user = User.create! I can: Event.create!(:historizable => user) But I can't: Event.where(:historizable => user) # Mysql2::Error: Unknown column 'events.historizable' in 'where clause' I have to do this instead: Event.where(:historizable_id => user.id, :historizable_type => user.class.name) Update Code that reproduces the issue: https://gist.github.com/fguillen/4732177#file-polymorphic_where_test

Rails: Load just one attribute not a whole model

☆樱花仙子☆ 提交于 2020-01-03 07:29:32
问题 Lets say I have a model, Foo , which is big and has lots of components. For a given Ajax query I'm only interested in one particular attribute, bar , which is a column in the foos table. Is there a simple way I could load just that attribute, and not bother with retrieving the rest of the record? For instance if all I want to know is the bar for Foo with id# _ _, how could I retrieve that? 回答1: You can return only specific columns by calling the select method with a string containing the