Posts by GuiGS • 503 points
20 posts
-
0
votes2
answers145
viewsA: Rails 5: Iugu and recurring payment
To create recurring subscriptions, you must link it to a plan.
-
5
votes1
answer47
viewsA: Doubt about creating "repeated" methods in Rails
You can define the methods dynamically using define_method: class User Role.all.each do |role| define_method("is_#{role.description}?") do self.role.id == role.id end end end…
-
0
votes2
answers1124
viewsA: How to rewrite correctly on Nginx?
In order for your application’s links to work, you need to set in the application the configuration relative_url_root, as explained in Deploy to a subdirectory of Rails Guides.…
-
1
votes1
answer62
viewsA: Nested Objects has_one Rails 4
The idea of using the accepts_nested_attributes_for is you do not need to create the album explicitly as you are doing in the create method. Just initialize the project with the parameters received…
-
1
votes1
answer515
viewsA: Group hashes by Ruby value and manipulate them
One way to do this manipulation is by using each_with_object Initiating with an empty hash. Then for each new brand found initializes a hash for the brand with zero counters and sums the purchases…
-
0
votes4
answers15818
viewsA: Add and Subtract value from same SQL column
It’s simpler than you’re trying, using IFF (from SQL Server 2012): select cod_Art, sum(IIF(Armazem = 4, -quant, quant)) from ccartigos_stock group by cod_art order by cod_Art…
-
6
votes2
answers775
viewsA: Generate random numbers
You are already thinking about how to implement in procedural form. From what I understand you want an array with the numbers 1 to 6 in random order. For this use the method Array#shuffle.…
-
1
votes2
answers1520
viewsA: How to model with inheritance a structure of People?
Uses normal relationships 1-1 (has_one/belongs_to) or 1-n (has_many/belongs_to) as appropriate. class Person < ActiveRecord::Base has_one :user has_one :client has_one :emplyee end class User…
-
0
votes1
answer304
viewsA: freight couriers
The problem is you’re making one redirect_to at the end. This causes a new request to be made to the server and the values are lost. You need to replace it with something like render 'checkout'.…
-
1
votes1
answer177
viewsA: Reset Password with Rails
I don’t know why you don’t want to use a Gem for this. We have the gift that implements this functionality and is very well tested. If you really don’t want to implement it yourself, I suggest you…
ruby-on-railsanswered GuiGS 503 -
1
votes1
answer193
viewsA: How to create block helpers in Rails?
You must use a block parameter and get the contents of the block with the method capture, as an example: def dropdown_menu(options = {}, &block) content_tag(:ul, options) do capture(&block)…
-
0
votes2
answers624
viewsA: Initializing instance variables in Activerecord
The way you did, assigning @test = 1 within the class, you are defining a class variable and not an instance variable. To do what you want, you can use the operator ||=. If you use the variable in…
-
1
votes1
answer108
viewsA: Rails 4 select Uniq
If you don’t need any information beyond the separate fields you can do: @itens = Item.select(:enrollment_id, :turma_id).uniq In this case, objects of type Item with id equal to nil. Or @itens =…
-
2
votes1
answer141
viewsA: History of editions of a Ruby model on Rails
There are multiple record versioning Gems. The best known is Papertrail. This Gem creates a table that saves the history of all changes of the monitored models, including can associate to the user…
-
1
votes3
answers176
viewsA: How to work with tests and fixtures in a bank that contains foreign key constraints in Rails?
A possible alternative solution is: instead of fixtures, work with factory_girl. And for cleaning the bank between tests, the Database Cleaner.…
-
2
votes2
answers443
viewsA: How to Best Do Conditional WHERE’s with Activerecord on Rails?
The where accepted a condition hash as parameter. So in that case you could just do: @pessoas = Pessoa.where(params) If you want to restrict the allowed filters you could use strong_parameters, for…
-
4
votes4
answers1643
viewsA: Is it really necessary to define constraints in the database?
Rails only does not guarantee data integrity. In particular, the validation of uniqueness does not guarantee that duplicate records will not be created in case of competing accesses. Rails Guides…
-
4
votes2
answers281
viewsA: Two-dimensional array in Ruby
Try to use less index manipulation, which makes code confusing. Ruby usually allows this. The code below creates elements in b for each index found in the elements of a. Then add to this element in…
-
1
votes1
answer650
viewsA: Display error message correctly
You must pass parameters to interpolation. t("errors.messages.restrict_dependent_destroy.many", record: "categorias") And to leave generic you can get the name through the object:…
-
1
votes1
answer520
viewsA: Model validation with Scope on Ruby on Rails
To do it that way, you really would need to create the column a_id on the table C. Thus, in addition to you can use the predefined validation of :uniqueness, you can create a unique key restriction…