rails-models

What is the proper way to use namespaces and reference models inheriting from other (mongoid/rails)?

依然范特西╮ 提交于 2020-01-02 14:31:42
问题 I have the model HandlingScenario who inherits from Scenario . Like this: ## models/scenario.rb class Scenario include Mongoid::Document end ## models/scenarios/handling_scenario.rb class Scenarios::HandlingScenario < Scenario include Mongoid::Document belongs_to :user, :inverse_of => :handling_scenarios # In the `User` model, the reference is `has_many :handling_scenarios` end But when I try to access the HandlingScenario class, I get into trouble: ➜ rails c Loading development environment

In Rails, how should I implement a Status field for a Tasks app - integer or enum?

本小妞迷上赌 提交于 2019-12-31 08:14:10
问题 For a Rails 3.0 Todo app, I have a Tasks model with a Status field. What's the best way to store the Status field data (field type) and still display a human-readable version in a view (HTML table)? Status can be: 0 = Normal 1 = Active 2 = Completed Right now I have this: Rails Schema Here: create_table "tasks", :force => true do |t| t.integer "status", :limit => 1, :default => 0, :null => false Rails Model Here: class Task < ActiveRecord::Base validates_inclusion_of :status, :in => 0..2,

Shared models between two Rails apps - what is the ideal solution for Workflow?

亡梦爱人 提交于 2019-12-29 02:45:09
问题 I am currently working on a Rails 3 project that is divided up into four parts: The public facing website The administration website/backend The models The API for third party data access As the models are shared between the three key components I want to keep them away from being in one main project, however each part needs access to the models, but I don't want to repeat the code and have different versions everywhere. Currently I have the model code in a gem, and in each project's Gemfile

Ruby on rails: Adding 2 references of a single model to another model

此生再无相见时 提交于 2019-12-23 05:25:15
问题 I'd like to know a proper way to implement my situation here in ruby on rails 4.0. Lets say I have 2 models named House and Order . My Order table should have two columns from and to both referencing a house model. What should my relations between these two models be in this case? Note: I do not require any reference to order model from house model. I would like to have something like this in my Order table t.references :house, as:from (this should create a column named from and should be of

Using a method within model, calling it from view

淺唱寂寞╮ 提交于 2019-12-23 02:49:29
问题 I have an Update model which belongs to users. To show all of one user's friends' Updates, I am doing something like: Update.where("user_id" => [array_of_friend_ids]) I know the "right" way of doing things is to create a method to create the above array. I started writing the method but it's only half-working. Currently I have this in my user model: def self.findfriends(id) @friendarray = [] @registered_friends = Friend.where("user_id" => id) @registered_friends.each do |x| @friendarray << x

Ruby on Rails pass id to new create form

徘徊边缘 提交于 2019-12-22 10:52:50
问题 Ok, I've searched high and low, read tutorials, watched videos and I am still not getting any where with this. I've read similar questions here, but questions were more complex or lacked answers - so here goes... I have models Account and Invoice. When showing an Account, I'd like a link to 'Create new invoice' which relates to that account. (Later I'd actually like a select field to choose an Account when creating an Invoice, but I'll leave that to another excruciation). Here are my models..

Rails Models relationships

别等时光非礼了梦想. 提交于 2019-12-22 01:16:25
问题 Hello fellow developers! Recently I've been playing with Rails 3.0 and after quite a bit of research I'm kinda stuck. I want to know what approach or solution is the best in my case(I couldn't find an answer yet). So what I'm trying to achieve is simple and straight forward. I want to do something like this: class User < ActiveRecord::Base has_many :feeds has_many :casts, :through => :feeds end class Feed < ActiveRecord::Base has_many :users has_many :casts end class Cast < ActiveRecord::Base

Rails Updating Data in one Model from another Model's Controller

◇◆丶佛笑我妖孽 提交于 2019-12-21 06:27:00
问题 I have a User model that has billing_id. I have Order model that runs transactions with a payment gateway which returns a billing id I'd like to save into the billing_id column on the User model. I think I am getting the basics of MVC architecture mixed up. I am under the impression that UsersController and OrdersController update data of their respective tables. Does that mean that if I have something returned from OrdersController such as a billing id, there is no other way of saving that

Rails: Invalid single-table inheritance type error

喜夏-厌秋 提交于 2019-12-20 10:29:07
问题 So, I am working on migrating this php site with an existing database which I cannot change over to Rails. There is a table: Quotes with a column named type . Whenever I try and create a model of this and set the type, it tells me the following error: ActiveRecord::SubclassNotFound (Invalid single-table inheritance type: HOME is not a subclass of Quotes) I don't understand why it thinks its inheriting because it's not supposed to. My create method looks like this: quote = Quotes.create( agent

Two different relations between same two models

谁都会走 提交于 2019-12-13 05:52:18
问题 I have two models: users and emails . I have separated this tables because we want the user to be able to have many emails in the same user account, and also easily check uniqueness of email values among accounts. primary_email_id is a FK of a unique email , but also a user has many emails . How do I say that in the rails model? I was trying class User < ActiveRecord::Base # relations has_many :emails has_one :primary_email end … class Email < ActiveRecord::Base # relations belongs_to :user