activerecord

How to change HTML output based on values in Rails

家住魔仙堡 提交于 2019-12-25 02:09:35
问题 I am trying to simply add an HTML class if a database value returned is a specific number. Here is what I tried: <ul class="dog-sizes-list"> <li <%= if given_size == 1 then puts "class='active'" end %>><img src="/assets/dogs/dog-icon.png" width="30px" /></li> <li <%= if given_size == 2 then puts "class='active'" end %>><img src="/assets/dogs/dog-icon.png" width="40px" /></li> <li <%= if given_size == 3 then puts "class='active'" end %>><img src="/assets/dogs/dog-icon.png" width="50px" /></li>

Getting no method error when trying to read attribute from join model

一个人想着一个人 提交于 2019-12-25 02:07:31
问题 I have projects and users joined via has_many :through join model called ownerships. Each ownership has an owner_type attribute that I want to access. I want to have a way to pass a user_id to a project model and get the owner_type via the ownership join model. And visa versa I want to pass a project_id to a user model and get the owner_type via the ownership join model. Here is what I have: class Project < ActiveRecord::Base attr_accessible :name, :description has_many :ownerships has_many

How to Show Sum of Column in Rails

痴心易碎 提交于 2019-12-25 01:46:02
问题 I want to display the sum of a column name "money" in my model Earning. In the rails console, I can easily get the sum I want Earning.sum(:money) and it shows the sum. earnings_controller.rb def index @earnings = Earning.sum(:money) end index.html.erb <p id="notice"><%= notice %></p> <h1>Earnings</h1> <table> <thead> <tr> <th>Money</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @earnings.each do |earning| %> <tr> <td><%= earning.money %></td> <td><%= link_to 'Show', earning %></td> <td>

Rails normalize before find_or_create

泄露秘密 提交于 2019-12-25 01:24:49
问题 I have a model with an address field that needs to be unique: class AddressMeta < AR::Base def normalize # normalize and store full_address end validates_uniqueness_of :full_address after_validation :normalize end I am passing addresses through a geocoding API to ensure that they are valid and normalized before storing. The trouble I'm running into is that I also want addresses to be unique, so there's only a single record per unique address string. Take the following addresses: 101 E 1st St

Using ActiveRecord for tables that aren't named as plurals

99封情书 提交于 2019-12-25 01:20:03
问题 I'm trying to get a basic rails app to read data out of a MS SQL Server database. In the tutorial I'm reading (snipped below), it mentions that the tables need to follow certain rules, such as the name being a pluralized and underscored, etc etc. None of the tables in my database follow that convention and that can't be changed. Is this still true? If so, what do I need to read to understand how to communicate with such tables? SNIPPET http://coding.smashingmagazine.com/2009/03/27/ultimate

Advice on RoR database schema and associations

倾然丶 夕夏残阳落幕 提交于 2019-12-25 01:07:18
问题 New to RoR, but am having a stab at building an app - and I'm looking for some advice on my db structure. I've got 4 models/tables: Users > Clients > Jobs > Tasks The app will work as follows: Users will login They can add Clients They can add Jobs to Clients They can add Tasks to Jobs So, Tasks belong to Jobs, Jobs belong to Clients, and Clients belong to Users. I want to query the DB for any one of a Client, a Job, or a Task and also make sure that it belongs to the currently logged-in User

Position of object in database

岁酱吖の 提交于 2019-12-25 00:42:21
问题 I have got model Team and I've got (i.e.) team = Team.first :offset => 20 . Now I need to get number of position of my team in db table. I can do it in ruby: Team.all.index team #=> 20 But I am sure that I can write it on SQL and it will be less expensive for me with big tables. 回答1: Assuming the order is made by ID desc: class Team < ActiveRecord::Base def position self.class.count(:conditions => ['id <= ?', self.id]) end end 回答2: I'm not sure I'm following along, but will position value be

has_one with more than one possible foreign key column

那年仲夏 提交于 2019-12-25 00:18:23
问题 I have a Family class that includes a mother_id and a father_id . From the Family model's perspective, it's important to know which parent is the mother and which is the father, but the mother and father as Residents have all the same attributes (i.e. database columns). So ideally, I'd like my model files to look like this: class Resident < ActiveRecord::Base has_one :family, :dependent => :nullify, :foreign_key => :father_id has_one :family, :dependent => :nullify, :foreign_key => :mother_id

using end as column name

≡放荡痞女 提交于 2019-12-24 22:32:21
问题 I'm maintaining a rails 2.1 application that has some unfortunate choices for column names. For instance, an Event has a start and an end date. Instead of using start_at and end_at the original design uses start and end . Of course this leads to def end read_attribute(:end) || 1.hour.from_now end I'm surprised this even parses. Is this legal ruby? The real issue is that erb is blowing up with 'stack level too deep' when running a backgroundrb job to send the reminder emails. The template is <

How to query all records that don't have a specific association?

半城伤御伤魂 提交于 2019-12-24 21:43:39
问题 If I want a list of all the shops that are open on Sunday, I do Shop.includes(:opening_times).where("opening_times.day =?", 'Sunday') Is there any way to get a list of all the shops that are closed on Sundays? That is, all the shops that are not associated with a record where the day column is 'Sunday'? 回答1: sun_open = Shop.includes(:opening_times).where("opening times.day = ?", "Sunday").ids; sunday_closed = Shop.where("id NOT IN (?)", sunday_open); 来源: https://stackoverflow.com/questions