rails-activerecord

Many to many through: Going from Associations::CollectionProxy to AssociationRelation

丶灬走出姿态 提交于 2019-12-25 05:06:18
问题 I have a many to many through relationship with users favoriting clients (Through favorites.rb) and I want to know how to turn a CollectionProxy into an AssociationRelation, so I can get a relation with all the clients that are the users' favorites. Or just simply, how to get them all in a AssociationRelation - doesn't have to be turned into from the CollectionProxy. EDIT: What I'm looking for is simply a relation that lists all the clients. I realize I asked the question differently. How do

ActiveRecord column decimal default value

耗尽温柔 提交于 2019-12-25 03:57:20
问题 I have a tableless class defined like this: class MyClass class MyClassRules < ActiveRecord::Base column :a, :integer column :b, :boolean column :c, :datetime column :d, :decimal, :precision => 10, :scale => 4 column :e, :string end end The object of type MyClassRules gets automatically built when I instantiate an object of type MyClass , and it is stored serialized in a single column in the my_class table called rules and accessible from my_obj.rules . So I can also make calls like my_obj

add has_many and belongs_to migration from command line

不打扰是莪最后的温柔 提交于 2019-12-25 02:23:01
问题 I generated two models and now want to implement active record associations. I have Designers and Items. An Item belongs to a Designer and a Designer has many Items. My models look like this: app/models/item.rb: class Item < ActiveRecord::Base belongs_to :designer validates :designer_id, presence: true end app/models/designer.rb: class Designer < ActiveRecord::Base has_many :items, dependent: :destroy end Even after I run rake db:migrate my migrations don't reflect the new relationship. They

Rails 4, retrieved many records with Model.where(related_model_id: 1), how to show each one in an individual view?

不羁岁月 提交于 2019-12-24 21:39:51
问题 Ok so I'm working on a quiz as part of my app. I have three nested models Quiz, Question, Answer. class Quiz < ActiveRecord::Base belongs_to :video has_many :questions has_many :answers, through: :questions accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, allow_destroy: true end class Question < ActiveRecord::Base belongs_to :quiz has_many :answers accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, allow_destroy:

Why am I getting strong parameter errors in the Rails Console in rails 5?

我怕爱的太早我们不能终老 提交于 2019-12-24 21:26:17
问题 I want to call this in my console ( ap is the awesome print gem): ap Purchase.last(10) but I get this error: ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash It works like this: irb(main):020:0> ap Purchase.last #<Purchase:0x00007f86b792a320> { :id => 28445, :user_id => 10177, :product_id => nil, :product_type => nil, :price => 9.0, :gateway_code => nil, :gateway_msg => nil, :gateway_response => nil, :created_at => Fri, 18 May 2018 22:20:10 UTC +00:00,

Heroku - ActiveRecord::StatementInvalid (PG::Error: ERROR: column “requested” does not exist

我们两清 提交于 2019-12-24 13:33:32
问题 I have an absense.html.erb which works fine in development and production. However when I try to access the page I get the following error: 2013-03-25T18:43:43+00:00 app[web.1]: Started GET "/absence" for 77.100.90.77 at 2013-03-25 18:43:43 +0000 2013-03-25T18:43:43+00:00 app[web.1]: 2013-03-25T18:43:43+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::Error: ERROR: column "requested" does not exist 2013-03-25T18:43:43+00:00 app[web.1]: LINE 1: ...LECT "holidays".* FROM "holidays" WHERE

Rails & Devise: Two-step confirmation params error

橙三吉。 提交于 2019-12-24 12:06:22
问题 This is a continuation of my woes and drama ... Run through: I can enter my email to register! I can click the confirmation link! I'm taken to the correct page to confirm: http://localhost:3000/user/confirmation?confirmation_token=jxOZQnyixE1PvnrptnYO Here's the problem...once I submit the form, I get this error: ActiveRecord::RecordNotFound in ConfirmationsController#confirm_account {"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"qUk6EDoR6N+V0h5O/jLKNZtl0hiaN/g9Gd5YdI2QhIU=", "user"

Dealing with associations in rails for has_many through…but two join tables deep

风格不统一 提交于 2019-12-24 11:33:52
问题 Edit 4. Something in my explanation is just not going right... attached is a (very poorly done graphic) that shows the tables and how the schema is, and then my main question is given what I have that a provider is joined via a group location id via the provider_location table, which the group location holds the group and ultimately the group name. This is what I want to know, building a table like this how do I get to the group name? all the way from a provider. It is like I need a has_many

Update a legacy “ID” column that isn't the primary key with ActiveRecord

早过忘川 提交于 2019-12-24 10:31:39
问题 I'm using Rails and the activerecord-sqlserver-adapter gem to try and add data to a legacy MS SQL database whose dbo.Condition table has a primary key called ConditionSeq and a foreign key column ID that stores the user ID. class Condition < ActiveRecord::Base # using lowercase_schema_reflection = true self.table_name = :condition self.primary_key = 'conditionseq' end Every time I write Condition.new(conditionseq: nil, id: 12345) (or even Condition.new(id: 12345) ), hoping to let MS SQL auto

ActiveRecord anonymous scope

你说的曾经没有我的故事 提交于 2019-12-24 05:18:06
问题 I am learning about anonymous scope from watch RailsCast video. When I try it myself, it seems like the statement: scope = User.scoped immediately queries the DB with SQL statement: User Load (3.2ms) SELECT `users`.* FROM `users` Before I even have a chance to chain conditions. This is obviously very inefficient and it's not happening when the author in the video does it. What am i missing? Also, at what point does the scope know that I am done chaining conditions and it's time to perform the