Questions tagged [rails-models]

a component of the Rails framework that holds the state of an object and is responsible for enforcing business rules and persisting the object.

367 questions
2
votes
2 answers

How to eager load child model's sum value for ruby on rails?

I have an Order model, it has many items, it looks like this class Order < ActiveRecord::Base has_many :items def total items.sum('price * quantity') end end And I have an order index view, querying order table like this def index …
Fang-Pen Lin
  • 13,420
  • 15
  • 66
  • 96
2
votes
1 answer

Turning a Rails AssociationRelation into a CollectionProxy

Say I have these classes: class Zoo < ActiveRecord::Base has_many :animals end class Animal < ActiveRecord::Base; belongs_to :zoo end class Lion < Animal; end class Tiger < Animal; end I can easily do a_zoo.animals but moreover a_zoo.animals…
whatyouhide
  • 15,897
  • 9
  • 57
  • 71
2
votes
2 answers

search the records using two keys in two tables in rails

I have models named User and Service User model has the following columns: user_name and location_name Service model has the following columns: user_location and service_name Now i want to search for the Users who are residing in a particular…
monu
  • 698
  • 4
  • 10
2
votes
1 answer

more than one entry saved on has_one association rails

I am trying to create a has_one association among two model. class User < ActiveRecord::Base has_one :emergency_contact end class EmergencyContact < ActiveRecord::Base belongs_to :user end when i try to test it through rails console more than…
2
votes
1 answer

Rails: Use reload! in controller

I'm creating a rails code that could add/remove field of a model. I've a model Inventory, where I could add a list of fields as below: def update_new_fields @fieldnames = params["fieldnames"] @fieldnames.each do |fieldname| …
Gavin Yap
  • 642
  • 12
  • 26
2
votes
1 answer

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

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…
Cjoerg
  • 1,271
  • 3
  • 21
  • 63
2
votes
2 answers

How to join model with attribute of other (related) model (plotting data from child)

In an app using the chartkick gem: I have the following database table relations: class User < ActiveRecord::Base has_many :games contains id, email end class Game < ActiveRecord::Base has_many :levels belongs_to :user #contains id,…
David Karlsson
  • 9,396
  • 9
  • 58
  • 103
2
votes
1 answer

Create a select from existing or create new in a form Rails 3

So I'm trying to set a name attribute of organizations and create a new organization or choose from a previously existing organization from the same form. I've tried to follow Ryan Bates' railscast on the topic here:…
2
votes
1 answer

How to test Model Errors with Mocha and rspec?

I have this problem related to testing model errors with Mocha: This is my controller (Api / Artist controller): class Api::ArtistsController < ApplicationController respond_to :json def create artist = Artist.new(params[:artist]) …
diegocst90
  • 61
  • 7
2
votes
3 answers

How to display this instance method from rails model?

I have two rails models, Usage & Price. So far they look like this: class Usage < ActiveRecord::Base has_one :price def spend usage.amount * price.amount end end and class Price < ActiveRecord::Base belongs_to…
KevL
  • 403
  • 6
  • 15
2
votes
1 answer

Get variable from another table in ROR models

I am trying to get a user who has not filled out their profile to redirect to the edit page. I have two tables - Users (Devise handles) and Profiles. Profiles has a row called profile_complete In my user model I am trying to define a method called…
user1847224
2
votes
1 answer

How does Rails automatically load ModelHelper into Model

I'm interested in how Rails automatically loads the modules in app/helpers into models. To wit: when app/helpers/widget_helper.rb exists and contains WidgetHelper that module is automatically loaded into the Widget model. I have need of a directory…
JohnMetta
  • 18,782
  • 5
  • 31
  • 57
2
votes
1 answer

Devise: Nested sign up form when the user and profile are separate models

I'm and usings devise for my authentication and would like to keep the details of my users in a separate model called profile. Profile contains information like first and last name. I want to be able to have a single sign up form that will be able…
Aaron Dufall
  • 1,177
  • 10
  • 34
2
votes
1 answer

Updating 2 models at once with same attributes in ruby on rails

i hav this in my store_opening_stock form <%= simple_form_for @store_opening_stock do |f| %> <%= f.error_messages %>

<%= f.hidden_field :user_id, :value => current_user.id %> <%= f.label :employee_id %> <%=…

bharath
  • 623
  • 7
  • 30
1
vote
2 answers

Rails: two models with one-to-one relationship, one controller

I've created a simple webapp with user authentication. I've made two models: Users for user authentication and Details for additional user details. They are associated with one-to-one relationship. I'm having problems with updating two models from…