ruby - Rails create one or create multiple records from a range -
I have a class called IP, which has : address
attribute, and two virtual features: : start_ip
and : end_ip
class IP & lt; ActiveRecord :: Base # The address attribute is saved in the database #start_ip and end_ip is the virtual feature, my form has been used in attr_accessor: start_ip,: end_ip end
I get a form Enter a single IP (in: address field) or a range of addresses in the field for my virtual attributes (: start_ip and: end_ip) that I have entered.
If I enter an IP then everything works fine: Address field.
If I enter multiple addresses (say that it spans 5 addresses), then I want to create an IP record for each address in the category, and I think it is the most to reject it It may well be that after entering a category as my current record, its address: address field will not be there.
So I think I already need a save callback:
before_save: range_given? Def range_life? Make it! ([@ Start_ip .. @ end_ip]) End
But this is not exactly right.
Can I currently set an immediate (and certified) record, and instead set 5 new records which range from the IP address for each category?
In the create
action in your controller, you can type something like this: / P>
(params [: start_ip] .. params [: end_ip]). Do each. IP | IP.create! (Address: IP) End
Add the code you need.
PD Perhaps you should reconsider your domain model, because IP address is different from IP range
Comments
Post a Comment