rails-models

Rails - Model Doubt

冷暖自知 提交于 2019-12-13 05:16:47
问题 Given the fact that I have models like this: class Person has_many :owned_groups, :class_name => "Group", :foreign_key => :owner_id has_many :owned_group_memberships, :through => :owned_groups, :source => :group_memberships has_many :group_memberships, :foreign_key => "member_id" has_many :groups, :through => :group_memberships end class GroupMembership belongs_to :member, :class_name => 'Person' belongs_to :group end class Group belongs_to :owner, :class_name => "Person" has_many :group

RAILS User hierarchies; classes, associations. Polymorphic?

荒凉一梦 提交于 2019-12-12 09:52:23
问题 I'm amazed at how hard it is to still find a definitive answer to this: it seems so common that I must be looking at it all wrong. We have Users, who's authorization roles run something like ROLES = %w[admin moderator teacher student banned] It's commonly recommended to use a ROLES field and Single Table Inheritance to (as here) class User < ActiveRecord::Base end class Student < User end class Teacher < User end But this puts all the data in one table. What if we have a good bit of data

Rails: New entry to a model that belongs_to two other

这一生的挚爱 提交于 2019-12-12 04:09:41
问题 In my app, a Product has_many opinions, which are written by many clients. Here are models with associations: class Product < ActiveRecord::Base attr_accessible :name, :desc, :price has_many :opinions end class Client < ActiveRecord::Base attr_accessible :name, :bio has_many :opinions end class Opinion < ActiveRecord::Base attr_accessible :rate, :comment belongs_to :client, :product end In parameters, I have the evaluation invitation id, which helps me to get both product_id and client_id (so

Ruby on rails 3 - Join many models to update a field of one model using attributes from another model non related directly

主宰稳场 提交于 2019-12-12 01:26:44
问题 I have a problem trying to update an attribute of one model using the value from another model, but those two models are not related. My models are: class Order < ActiveRecord::Base has_many :order_details has_many :waybills end class OrderDetail < ActiveRecord::Base belongs_to :order, :foreign_key => "order_id" belongs_to :product, :foreign_key => "product_id" end class Waybill < ActiveRecord::Base has_many :purchases belongs_to :order, :foreign_key => 'order_id' end class Purchase <

Calculate Percent of total from scoped records and display virtual attributes (Rails / Active Record)

痴心易碎 提交于 2019-12-12 01:11:34
问题 Create a scope, maybe something like this.. scope :mv, select('*,quantity*market_price as market_value, quantity*market_price/sum(quantity*market_price) as percent') that creates two virtual attributes, market_value and percent. The problem I am having is creating the percent with the sum() included. If I add sum(), the scope returns one record. I need to calculate the percent of market_value relative to the total market value of the scoped record set. example: 1, market_value: 100, percent:

NoMethodError in Orders#new

别来无恙 提交于 2019-12-11 20:27:38
问题 I'm receiving an error when it comes to entering data into my table. undefined method `price' for # I was able to add the Price into my Order table, but when it comes to adding new data. I receive a, NoMethodError in Orders#new. I went into my text editor and deleted the highlighted piece of code, but the field to enter a price disappears all that is left is the title, Price. Showing /Users/nncyvallejo90/Documents/springproject/app/views/orders/_form.html.erb where line #20 raised: undefined

Two controllers, one model — why is my app looking for a second model?

旧城冷巷雨未停 提交于 2019-12-11 11:39:52
问题 I have an Order model. Customers get a handful of consumer-friendly views that let them create and view their own orders, all backed by an Orders controller. Admins get the full range of views to create, edit, view, delete and manage orders, backed by a Purchases controller. As far as I can tell, the Purchases controller should only be speaking to the Order model, but the following error message makes me think it's looking for a non-existant Purchase model: ActiveRecord::StatementInvalid in

Rails 4 - best_in_place gem and many-to-many association

◇◆丶佛笑我妖孽 提交于 2019-12-10 09:44:53
问题 I am struggling to add the ability to update a contact languages using best_in_place (a jQuery inplace-editor gem). I have a contact object, a language object and a has_and_belongs_to_many :languages relationship between contacts and languages. Therefore, a has_and_belongs_to_many implied a join table between the contacts and the languages tables. To sum up quickly, I have something like this: contacts contacts_languages languages +------+--------+ +------------+-------------+ +------+-------

Query that joins child model results item erroneously shown multiple times

佐手、 提交于 2019-12-08 18:18:27
I have the following models, each a related child of the previous one (I excluded other model methods and declarations for brevity): class Course < ActiveRecord::Base has_many :questions scope :most_answered, joins(:questions).order('questions.answers_count DESC') #this is the query causing issues end class Question < ActiveRecord::Base belongs_to :course, :counter_cache => true has_many: :answers end class Answer < ActiveRecord::Base belongs_to :question, :counter_cache => true end Right now I only have one Course populated (so when I run in console Course.all.count , I get 1 ). The first

Question about Association and Models on Rails

被刻印的时光 ゝ 提交于 2019-12-08 13:40:53
问题 I have been working on a project recently where a Player can create a Team and be the Team Owner, but a player as well can be part of the Team by a separate table, named Squad. class Player has_many :teams has_many :squads end class Squad belongs_to :player belongs_to :team end class Team belongs_to :owner, :class_name => "Player" has_many :squads has_many :players, :through => "squads" end I don't know if this is all I need to make what I want, but I just can't figure out. How can I make the