rails-models

has_one and has_many in same model. How does rails track them?

我与影子孤独终老i 提交于 2019-12-03 00:12:40
I a little confused about how this work even if it works properly. I have a model that has two association to the same other model. Company has an owner and company has many employees of the class users. here is my company model: class Company < ActiveRecord::Base validates_presence_of :name has_many :employee, :class_name => 'User' has_one :owner, :class_name => 'User' accepts_nested_attributes_for :owner, :allow_destroy => true end here is my user model: class User < ActiveRecord::Base include Clearance::User attr_accessible :lastname, :firstname #other attr are whitelisted in clearance gem

Rails: Invalid single-table inheritance type error

北战南征 提交于 2019-12-02 22:54:51
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_id: agent.id, client_id: client.id, type: 'HOME', status: 0, date_created: DateTime.now ) If I comment

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

拥有回忆 提交于 2019-12-02 14:18:20
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, :message => "{{value}} must be 0, 1, or 2" Rails View Here: <h1>Listing tasks</h1> <table> <tr> <th>Status

How to create an association between two rails models

孤者浪人 提交于 2019-12-01 12:39:35
This is a newbie question, but I'm still learning how to create an association between two models in rails. I have a user model and a journal_entry model. The journal entries belong to the user and the user has_many journal entries. I've created migrations that look like this: class AddJournalEntriesToUsers < ActiveRecord::Migration def change add_column :journal_entries, :user_id, :integer end end class AddIndexToJournalEntries < ActiveRecord::Migration def change add_index :journal_entries, [:user_id, :created_at] end end Here's what my User model looks like: class User < ActiveRecord::Base

How to create an association between two rails models

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 09:57:32
问题 This is a newbie question, but I'm still learning how to create an association between two models in rails. I have a user model and a journal_entry model. The journal entries belong to the user and the user has_many journal entries. I've created migrations that look like this: class AddJournalEntriesToUsers < ActiveRecord::Migration def change add_column :journal_entries, :user_id, :integer end end class AddIndexToJournalEntries < ActiveRecord::Migration def change add_index :journal_entries,

Nested attributes for belongs_to association rails

久未见 提交于 2019-11-30 11:45:39
I have two models, Complaint and Company. Complaint belongs_to and accepts_nested_attributes for Company, and Company has_many Complaints. # Models class Complaint < ActiveRecord::Base attr_accessible :complaint, :date, :resolved belongs_to :user, :class_name => 'User', :foreign_key => 'id' belongs_to :company, :class_name => 'Company', :foreign_key => 'id' has_many :replies accepts_nested_attributes_for :company end class Company < ActiveRecord::Base attr_accessible :name has_many :complaints, :class_name => 'Complaint', :foreign_key => 'id' has_many :branches, :class_name => 'Branch',

How to add sequences to a migration and use them in a model?

限于喜欢 提交于 2019-11-30 11:09:53
I want to have a " Customer " Model with a normal primary key and another column to store a custom "Customer Number". In addition, I want the db to handle default Customer Numbers. I think, defining a sequence is the best way to do that. I use PostgreSQL. Have a look at my migration: class CreateAccountsCustomers < ActiveRecord::Migration def up say "Creating sequenze for customer number starting at 1002" execute 'CREATE SEQUENCE customer_no_seq START 1002;' create_table :accounts_customers do |t| t.string :type t.integer :customer_no, :unique => true t.integer :salutation, :limit => 1 t

Nested attributes for belongs_to association rails

蓝咒 提交于 2019-11-29 17:44:39
问题 I have two models, Complaint and Company. Complaint belongs_to and accepts_nested_attributes for Company, and Company has_many Complaints. # Models class Complaint < ActiveRecord::Base attr_accessible :complaint, :date, :resolved belongs_to :user, :class_name => 'User', :foreign_key => 'id' belongs_to :company, :class_name => 'Company', :foreign_key => 'id' has_many :replies accepts_nested_attributes_for :company end class Company < ActiveRecord::Base attr_accessible :name has_many

How to have multiple conditions in a named scope?

十年热恋 提交于 2019-11-29 11:06:05
I have a User model. I can check whether a User is an admin by doing a_user.try(:admin?) . I'd like to define a named scope that gets all Users updated within the last X minutes who are not admins. So far I have: scope :recent, lambda { { :conditions => ['updated_at > ?', 5.minutes.ago] } } This successfully gets all Users updated within the last 5 minutes, but how do I incorporate the admin check? I don't know how to call try() on an instance of a User inside the scope... if admin column in users table is a boolean, scope :recent, lambda { :conditions => ['updated_at > ? AND admin != ?', 5

Rails has_one :through association

纵饮孤独 提交于 2019-11-28 16:57:02
Rails has a has_one :through association that helps set up a one-to-one association with a third model by going through a second model. What is the real use of that besides making a shortcut association, that would otherwise be an extra step away. Taking this example from the Rails guide : class Supplier < ActiveRecord::Base has_one :account has_one :account_history, :through => :account end class Account < ActiveRecord::Base belongs_to :supplier has_one :account_history end class AccountHistory < ActiveRecord::Base belongs_to :account end might allow us to do something like: supplier.account