ruby-on-rails-2

setting primary key for a database not named “:id”

大城市里の小女人 提交于 2019-12-01 06:34:05
I am using: rails 2.3.5 ruby 1.8.7 and Windows 7 Home Basic I was given a database and I connected it to rails, having no problems reading and getting data from it. Now what I want to do is add some functionality in it (add, edit and delete) but when I try to set my primary key to the table's primary key (ProductCode) by doing this code: class Product < ActiveRecord::Base self.primary_key :ProductCode end I got this error when doing a @products = Product.find(:all, :limit => 10) : ArgumentError in PosController#index wrong number of arguments (1 for 0) How can I solve this ? Here's my

setting primary key for a database not named “:id”

强颜欢笑 提交于 2019-12-01 04:21:45
问题 I am using: rails 2.3.5 ruby 1.8.7 and Windows 7 Home Basic I was given a database and I connected it to rails, having no problems reading and getting data from it. Now what I want to do is add some functionality in it (add, edit and delete) but when I try to set my primary key to the table's primary key (ProductCode) by doing this code: class Product < ActiveRecord::Base self.primary_key :ProductCode end I got this error when doing a @products = Product.find(:all, :limit => 10) :

How to Install PCRE Development Headers on Mac OSX

坚强是说给别人听的谎言 提交于 2019-11-30 22:36:12
问题 I just upgraded my MacBook Pro to Mavericks and my local Ruby on Rails development environment isn't running straight off the bat, when I visit localhost I see It works! and remembered I needed to start Phusion Passenger, so when I run passenger start it checks all the prerequisites and fails when it gets to the PCRE Development Headers: * Checking for PCRE development headers... Found: no It tells me to go to http://www.pcre.org/ to download them so I downloaded 8.33 from here which went to

Adding extra run-time attribs to an activerecord object

依然范特西╮ 提交于 2019-11-30 11:48:33
I have an Agent model which gets its attributes from the underlying database table. However for one particular controller action I would like to add some 'temporary' attributes to the Agent records before passing them on to the view. Is this possible? Yes, you can extend your models on the fly. For example: # GET /agents # GET /agents.xml def index @agents = Agent.all # Here we modify the particular models in the @agents array. @agents.each do |agent| agent.class_eval do attr_accessor :foo attr_accessor :bar end end # And then we can then use "foo" and "bar" as extra attributes @agents.each do

Display a checkbox list instead of multiple select

喜欢而已 提交于 2019-11-30 06:56:53
I have a model MyModel with a serialized attribute a , describing an array of symbols. This code works : <% form_for @my_model do |f| %> <%= f.select :a, MyModel::AS, :multiple => true) %> <% end %> The parameters are correct : { :my_model => { :a => [:a_value1, :a_value2] } } I want to transform this multiple select into a set of checkboxes, like this : <% form_for @my_model do |f| %> <% MyModel::AS.each do |a_value| <%= f.check_box(:a_value) %> <% end %> <% end %> It works too, but the parameters are not the same at all : { :my_model => { :a_value1 => 1, :a_value2 => 1 } } I think of 2

Routing with an optional parameter

霸气de小男生 提交于 2019-11-30 01:09:26
I added in the route file: map.show_book "/show_book/:name/year/:year", :controller => "book", :action => "show_version" I also added: map.show_book "/show_book/:name", :controller => "book", :action => "show_version" to show the latest book without specifying the year. But it doesn't work, it cannot find the route in "show_book/NAME" if I don't pass the year. Do you have some ideas why it doesn't work ? THANKS ! PS. I know that I can use year as parameter with "?year=XXXX", but I want to use the year as a part of the URL Benoit Garret Put the optional parts between parenthesis : map.show_book

Polymorphic Assocations using Integer ID type fields

有些话、适合烂在心里 提交于 2019-11-29 09:52:56
I have a table Foo that has a polymorphic belongs_to association called bar . The foos table has the standard bar_id column. However, instead of a string-based bar_type column, I have an integer bar_type_id column. This column references the id column in the table bar_types . bar_types.name holds the name of the class that represents the class of the particular bar instance. Does Rails (ideally >=2.3.10) allow for this type of polymorphic association? We did it by overriding the association_class method in a new module and included it using the :extend option. Also created a integer to string

Display a checkbox list instead of multiple select

纵然是瞬间 提交于 2019-11-29 02:52:21
问题 I have a model MyModel with a serialized attribute a , describing an array of symbols. This code works : <% form_for @my_model do |f| %> <%= f.select :a, MyModel::AS, :multiple => true) %> <% end %> The parameters are correct : { :my_model => { :a => [:a_value1, :a_value2] } } I want to transform this multiple select into a set of checkboxes, like this : <% form_for @my_model do |f| %> <% MyModel::AS.each do |a_value| <%= f.check_box(:a_value) %> <% end %> <% end %> It works too, but the

Using named_scope with counts of child models

坚强是说给别人听的谎言 提交于 2019-11-29 02:38:00
I have a simple parent object having many children. I'm trying to figure out how to use a named scope for bringing back just parents with specific numbers of children. Is this possible? class Foo < ActiveRecord::Base has_many :bars named_scope :with_no_bars, ... # count of bars == 0 named_scope :with_one_bar, ... # count of bars == 1 named_scope :with_more_than_one_bar, ... # count of bars > 1 end class Bar < ActiveRecord::Base belongs_to :foo end I'm hoping to do something like Foo.with_one_bar I could write methods on the parent class for something like this, but I'd rather have the power of

Routing with an optional parameter

China☆狼群 提交于 2019-11-28 21:56:29
问题 I added in the route file: map.show_book "/show_book/:name/year/:year", :controller => "book", :action => "show_version" I also added: map.show_book "/show_book/:name", :controller => "book", :action => "show_version" to show the latest book without specifying the year. But it doesn't work, it cannot find the route in "show_book/NAME" if I don't pass the year. Do you have some ideas why it doesn't work ? THANKS ! PS. I know that I can use year as parameter with "?year=XXXX", but I want to use