has_many through additional (default) attribute values for through record on adding

7 views
Skip to first unread message

Nazar Matus

unread,
Dec 21, 2016, 6:03:40 PM12/21/16
to Ruby on Rails: Core
For example, I have a User, Achievement and AchievementDate.
class User < ApplicationRecord
  has_many :achievement_dates
  has_many
:achievements, through: :achievement_dates
end

And when I write add an achievement like this:
user.achievements << achievement

I want through record to be initialized with current date. So I want something like this:
class User < ApplicationRecord
  has_many :achievement_dates
  has_many
:achievements, through: :achievement_dates, initialize_with: :method_name

 
private

 
def method_name(achievement_date)
    achievement_date
.date = Date.current
 
end
end

Currently I could not find a good way to achieve such behavior in Rails 5.0.0.1, so please, give me some advice or feedback.

T.J. Schuck

unread,
Dec 21, 2016, 6:32:25 PM12/21/16
to rubyonra...@googlegroups.com
This mailing list (rubyonrails-core) is intended for discussion about development of the framework itself, not usage of it.

For support issues or usage questions like this one that are not issues with Rails itself, please post to the rubyonrails-talk mailing list (http://groups.google.com/group/rubyonrails-talk) or somewhere like StackOverflow using the `ruby-on-rails` tag (http://stackoverflow.com/questions/tagged/ruby-on-rails).  You can also drop in to the #rubyonrails channel on irc.freenode.net for realtime help.  You'll be more likely to get a quick answer using one of those options.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages