activerecord

RoR Active Record undefined method

天大地大妈咪最大 提交于 2019-12-25 03:26:24
问题 That is the error im getting: undefined method `attr_accessible' for # after following a tutorial, my code is: User.rb: class User < ActiveRecord::Base attr_accessible :email, :password, :password_confirmation attr_accessor :password before_save :encrypt_password validates_confirmation_of :password validates_presence_of :password, :on => :create validates_presence_of :email validates_uniqueness_of :email users_conroller.rb: class UsersController < ApplicationController def new @user = User

adding class methods to ActiveRecord::Base

人走茶凉 提交于 2019-12-25 03:22:42
问题 I have created an instance method which is also a callback (if that makes sense) which does some stuff thats irrelevant. I would love to be able to just call: class Model < ActiveRecord::Base fix_camelcase_columns end Instead at the moment I have this: def after_find self.class.columns.each do |column| self.instance_eval("def #{column.name.to_underscore}; self.#{column.name}; end;") end end I would love to abstract this and use it on other classes. Any pointers? 回答1: Well, you can open up

Yii2: How to let ActiveRecord models only see certain database data?

99封情书 提交于 2019-12-25 03:07:49
问题 I'm trying to implement a multi-tenant application. I have different customers that are using the same code. But each customer gets its own entry script with its own configuration. The customer id is accessible by Yii::$app->params['customerId'] . Note: I have to distinguish the customers that way because there is no login/authentication. Anyway, assume the controller action is called and a customer id is given with the parameter. Now I have some tables and each has a column for the customer

rails. select records with max()

感情迁移 提交于 2019-12-25 03:05:08
问题 I have a table like this, and I want to return the top two person (by name) with the highest salary, and also the record with the corresponding salary. Here is the table id, name, salary 1, Tom, 200 2, Tom, 300 3, Bob, 400 4, Bob, 500 5, Alice, 600 6, Alice, 700 I used this command Employer.select("employers.*, max(employers.salary) as maxsalary").group("employers.name").order("maxsalary desc").limit(2) Desired return: id, name, salary 6, Alice, 700 4, Bob, 500 What I got seems to be like

What are the Rails's ActiveRecord database gems on JRuby

只谈情不闲聊 提交于 2019-12-25 02:57:54
问题 When running rails on JRUBY, database adapters have two different gems. Sql Server : sqlserver gem Vs activerecord-sqlserver-adapter gem Mysql : jdbcmysql gem vs activerecord-jdbcmysql-adapter gem Sqlite3 : jdbcsqlite3 gem Vs activerecord-jdbcsqlite3-adapter gem Postgresql : jdbcpostgresql gem Vs activerecord-jdbcpostgresql-adapter gem jdbc : jdbc gem vs activerecord-jdbc-adapter gem So question is, What is the difference between jdbcpostgresql and its long form activerecord-jdbcpostgresql

ActiveRecord/sqlite3 column type lost in table view?

↘锁芯ラ 提交于 2019-12-25 02:57:52
问题 I have the following ActiveRecord testcase that mimics my problem. I have a People table with one attribute being a date. I create a view over that table adding one column which is just that date plus 20 minutes: #!/usr/bin/env ruby %w|pp rubygems active_record irb active_support date|.each {|lib| require lib} ActiveRecord::Base.establish_connection( :adapter => "sqlite3", :database => "test.db" ) ActiveRecord::Schema.define do create_table :people, :force => true do |t| t.column :name,

ActiveRecord (Rails 4) determine index of record in query

北城以北 提交于 2019-12-25 02:37:35
问题 What I'm trying to do is determine what rank a sales person came in for a month as far as earning commission goes in the least amount of steps possible. I think it might be possible to do with a combination of ActiveRecord + some enumerable methods, but essentially I have a model that looks like the following: class Employee < ActiveRecord::Base validates :commission, presence: true end commission is just an integer. So in order to get all employees ordered from highest to lowest for

ActiveRecord: validates_associated does not work when updating model?

孤者浪人 提交于 2019-12-25 02:29:06
问题 I have 2 models: class Book < ActiveRecord::Base has_many :book_versions accepts_nested_attributes_for :book_versions, allow_destroy: true validates_associated :book_versions class BookVersion < ActiveRecord::Base has_many :collection_items has_many :collections, through: :collection_items belongs_to :book validates_presence_of :price, :isbn #<-- validates presence Everything works fine and dandy when I go to books/new and create a book with multiple book versions. The validations fire when I

change association from one to many to many to many

廉价感情. 提交于 2019-12-25 02:26:58
问题 I have two models lets suppose class A < ActiveRecord::Base has_many :bs end class B < ActiveRecord::Base belogns_to :a end now because of some system changes I need to convert this association to many to many some thing like this class A < ActiveRecord::Base has_and_belongs_to_many :bs end class B < ActiveRecord::Base has_and_belongs_to_many :as end OR class A < ActiveRecord::Base has_many :cs has_many :bs, through: :cs end class B < ActiveRecord::Base has_many :cs has_many :as, through: :cs

How should my form look in Rails?

落爺英雄遲暮 提交于 2019-12-25 02:13:35
问题 I have namespaced two models. One model Topic is parent to the Post model and Post shares a HABTM relationship with the Tag model. None of the models have validations. I would like to gather use a checkbox to set data on the Topic and Tag model while submtting the post in a single form. Any time I have tried however I run into this problem. Questions I have: How should I augment my form, controller and model to avoid the errors found in this post ? Do I need to declare my namespace in each