ruby-on-rails-3

Converting an SQL query to a Rails AREL query?

自作多情 提交于 2019-12-24 12:39:32
问题 I have a working SQL query thanks to the help of Erwin Brandstetter in my earlier question 'Order with a has_many relationship'. How would I turn this SQL into an ActiveRecords or AREL query for use in a scope? SELECT a.* FROM articles a LEFT JOIN ( SELECT DISTINCT ON (article_id) article_id, value FROM metrics m WHERE name = 'score' ORDER BY article_id, date_created DESC ) m ON m.article_id = a.id ORDER BY m.value DESC; The closest I have come is with a friends help... scope :highest_score,

rails order through count on other table

落爺英雄遲暮 提交于 2019-12-24 12:37:23
问题 I'm adding quiz functionality to the twitter app from the Hartl tutorial and have these Models: User is nearly the same as the tutorial: class User < ActiveRecord::Base has_many :followed_users, through: :relationships, source: :followed has_many :takens, dependent: :destroy has_many :questions, through: :takens end Taken is a table of Question ids to User ids: class Taken < ActiveRecord::Base belongs_to :user belongs_to :question end nothing interesting in Question: class Question <

How to link to a pdf from a png with Rails?

我与影子孤独终老i 提交于 2019-12-24 12:28:23
问题 I have an image of a map.png file on part of a page. I would like this image to be clickable, and then to download the pdf version of that image. I've been using this as a reference Rails 3.1, can't make link_to image_path?, but I'm not sure how to proceed. It looks like I also need to edit something with the way the page is routed. Thanks for all the help! 回答1: Do you have a route to the pdf download or is the file itself a static asset? You can use a standard link_to helper with an image

How do I dynamically generate a unique name for each option within an options_from_collection_for_select?

我怕爱的太早我们不能终老 提交于 2019-12-24 12:28:15
问题 I want to generate an option tag like this: <option name="option_1" value="1">Small</option> <option name="option_2" value="2">Medium</option> Using my call like this: <%= select_tag option.name, options_from_collection_for_select(option.option_values, "id", "name"), include_blank: true %> Gets me half-way there, by generating this: <option value="1">Small</option> <option value="2">Medium</option> How do I add the name attribute to the option tag, using this structure? Is that even possible?

Rails 3.2 with Devise: logout link stopped working

匆匆过客 提交于 2019-12-24 12:06:29
问题 The log out link in my app has been working fine, then it just stopped working. I haven't tested it for a while, but I haven't made any changes to anything related to it for a month! It is sending a GET request when it should be interpreted by the controller as a DELETE request. Here is my code: My ERB file in my view includes this line: <li><%= link_to('Log Out', destroy_user_session_path, :method => :delete)%></li> and also includes this: <%= javascript_include_tag "application" %> This is

rails: how do I build an active-relation scope to traverse many tables?

你。 提交于 2019-12-24 11:59:58
问题 I have these tables and relationships: user has_many projects project has_many tasks task has_many actions I would like to build a scope that allows me to select all of the current users actions, regardless of what project or task they belong to. Thanks 回答1: I don't think scopes are necessary for this if you use the nested_has_many_through plugin. class User < ActiveRecord::Base has_many :projects has_many :tasks, :through => :projects has_many :actions, :through => :tasks end class Project <

Why there is no assets/images directory in Rails 4.0 RC1

柔情痞子 提交于 2019-12-24 11:57:21
问题 I just discovered that there isnt a default directory assets/images when I generate a Rails 4 app (Rails 4 rc1 currently). Whereas in Rails 3.1/3.2 its generated. Please tell me why is that so? I mean I can create a directory in a second, but thats not the point. If I didnt get the reason for this on any announcement I am sorry, otherwise it makes no sense to me. Thank you 回答1: Just found out myself that there's no index.html in public any more either because it's done dynamically in Rails 4

belongs_to association and formtastic

£可爱£侵袭症+ 提交于 2019-12-24 11:53:25
问题 I have a model Product which has a belongs_to association with another model Type. In the product's form, I'm using formtastic to display a select tag with all the types available in the database, like this: <%= f.input :type %> The select is showing up OK, but each option of it is an object instance of the Type model as a string, for example: #<Type:0x00eff180c85c8> Instead of that, I'd like to display the 'title' attribute of it, like: Electronic Domestic ... Any ideas? 回答1: Try the member

Self-referential find in controller count relations

夙愿已清 提交于 2019-12-24 11:49:15
问题 I'm having real trouble pulling out a set of records that are self-referentially related to a user in order to show these on a user's 'show' page. Here's the idea: Users ( current_user ) rate the compatibility between two other users ( user_a and user_b ). They can rate compatibility either positively or negatively: rating two users "compatible" creates a positive_connection between user_a and user_b, and rating them "incompatible" creates a negative_connection . So there are models for

running scripts with bundle exec is slow [closed]

跟風遠走 提交于 2019-12-24 11:35:29
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . When running with bundle exec $ time bundle exec rails generate model student name:string age:number invoke active_record create db/migrate/20121215170617_create_students.rb create app/models/student.rb real 0m13.397s user 0m11.676s sys 0m0.597s Running directly $ time rails generate model student name:string